Skip to content

Commit e44e1b4

Browse files
authored
Initial plugin support for background downloads + content fetching (#5082)
* Added handleEventsForBackgroundURLSession and performFetchWithCompletionHandler handlers in FlutterAppDelegate to allow for plugins to perform background downloads and fetch small amounts of data opportunistically.
1 parent 78f8a0f commit e44e1b4

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

‎shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
2121
Defines a set of optional callback methods and a method to set up the plugin
2222
and register it to be called by other application components.
2323
*/
24-
@protocol FlutterPlugin<NSObject>
24+
@protocol FlutterPlugin <NSObject>
2525
@required
2626
/**
2727
Registers this plugin.
@@ -130,12 +130,28 @@ NS_ASSUME_NONNULL_BEGIN
130130
completionHandler:(void (^)(BOOL succeeded))completionHandler
131131
API_AVAILABLE(ios(9.0));
132132

133+
/**
134+
Called if this plugin has been registered for `UIApplicationDelegate` callbacks.
135+
136+
- Returns: `YES` if this plugin handles the request.
137+
*/
138+
- (BOOL)application:(UIApplication*)application
139+
handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
140+
completionHandler:(nonnull void (^)())completionHandler;
141+
142+
/**
143+
Called if this plugin has been registered for `UIApplicationDelegate` callbacks.
144+
145+
- Returns: `YES` if this plugin handles the request.
146+
*/
147+
- (BOOL)application:(UIApplication*)application
148+
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
133149
@end
134150

135151
/**
136152
Registration context for a single `FlutterPlugin`.
137153
*/
138-
@protocol FlutterPluginRegistrar<NSObject>
154+
@protocol FlutterPluginRegistrar <NSObject>
139155
/**
140156
Returns a `FlutterBinaryMessenger` for creating Dart/iOS communication
141157
channels to be used by the plugin.
@@ -211,7 +227,7 @@ NS_ASSUME_NONNULL_BEGIN
211227
Plugins are identified by unique string keys, typically the name of the
212228
plugin's main class.
213229
*/
214-
@protocol FlutterPluginRegistry<NSObject>
230+
@protocol FlutterPluginRegistry <NSObject>
215231
/**
216232
Returns a registrar for registering a plugin.
217233

‎shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ @interface FlutterAppDelegate ()
1111
@property(readonly, nonatomic) NSMutableDictionary* pluginPublications;
1212
@end
1313

14-
@interface FlutterAppDelegateRegistrar : NSObject<FlutterPluginRegistrar>
14+
@interface FlutterAppDelegateRegistrar : NSObject <FlutterPluginRegistrar>
1515
- (instancetype)initWithPlugin:(NSString*)pluginKey appDelegate:(FlutterAppDelegate*)delegate;
1616
@end
1717

@@ -210,6 +210,31 @@ - (void)application:(UIApplication*)application
210210
}
211211
}
212212

213+
- (void)application:(UIApplication*)application
214+
handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
215+
completionHandler:(nonnull void (^)())completionHandler {
216+
for (id<FlutterPlugin> plugin in _pluginDelegates) {
217+
if ([plugin respondsToSelector:_cmd]) {
218+
if ([plugin application:application
219+
handleEventsForBackgroundURLSession:identifier
220+
completionHandler:completionHandler]) {
221+
return;
222+
}
223+
}
224+
}
225+
}
226+
227+
- (void)application:(UIApplication*)application
228+
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
229+
for (id<FlutterPlugin> plugin in _pluginDelegates) {
230+
if ([plugin respondsToSelector:_cmd]) {
231+
if ([plugin application:application performFetchWithCompletionHandler:completionHandler]) {
232+
return;
233+
}
234+
}
235+
}
236+
}
237+
213238
// TODO(xster): move when doing https://github.com/flutter/flutter/issues/3671.
214239
- (NSObject<FlutterBinaryMessenger>*)binaryMessenger {
215240
UIViewController* rootViewController = _window.rootViewController;
@@ -286,11 +311,11 @@ - (void)addApplicationDelegate:(NSObject<FlutterPlugin>*)delegate {
286311
}
287312

288313
- (NSString*)lookupKeyForAsset:(NSString*)asset {
289-
return [FlutterDartProject lookupKeyForAsset:asset];
314+
return [FlutterDartProject lookupKeyForAsset:asset];
290315
}
291316

292317
- (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
293-
return [FlutterDartProject lookupKeyForAsset:asset fromPackage:package];
318+
return [FlutterDartProject lookupKeyForAsset:asset fromPackage:package];
294319
}
295320

296321
@end

0 commit comments

Comments
 (0)