Flutter iOS Embedder
FlutterAppDelegate.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
8 #import "flutter/shell/platform/darwin/ios/InternalFlutterSwift/InternalFlutterSwift.h"
16 
18 
19 static NSString* const kUIBackgroundMode = @"UIBackgroundModes";
20 static NSString* const kRemoteNotificationCapabitiliy = @"remote-notification";
21 static NSString* const kBackgroundFetchCapatibility = @"fetch";
22 
23 @interface FlutterAppDelegate () {
24  __weak NSObject<FlutterPluginRegistrant>* _weakRegistrant;
25  NSObject<FlutterPluginRegistrant>* _strongRegistrant;
26 }
27 @property(nonatomic, copy) FlutterViewController* (^rootFlutterViewControllerGetter)(void);
28 @property(nonatomic, strong) FlutterPluginAppLifeCycleDelegate* lifeCycleDelegate;
29 @property(nonatomic, strong) FlutterLaunchEngine* launchEngine;
30 @end
31 
32 @implementation FlutterAppDelegate
33 
34 - (instancetype)init {
35  if (self = [super init]) {
36  _lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
37  _launchEngine = [[FlutterLaunchEngine alloc] init];
38  }
39  return self;
40 }
41 
42 - (nullable FlutterEngine*)takeLaunchEngine {
43  return [self.launchEngine takeEngine];
44 }
45 
46 - (BOOL)application:(UIApplication*)application
47  willFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
48  return [self.lifeCycleDelegate application:application
49  willFinishLaunchingWithOptions:launchOptions];
50 }
51 
52 - (BOOL)application:(UIApplication*)application
53  didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
54  return [self.lifeCycleDelegate application:application
55  didFinishLaunchingWithOptions:launchOptions];
56 }
57 
58 // Returns the key window's rootViewController, if it's a FlutterViewController.
59 // Otherwise, returns nil.
60 - (FlutterViewController*)rootFlutterViewController {
61  if (_rootFlutterViewControllerGetter != nil) {
62  return _rootFlutterViewControllerGetter();
63  }
64  UIViewController* rootViewController = _window.rootViewController;
65  if ([rootViewController isKindOfClass:[FlutterViewController class]]) {
66  return (FlutterViewController*)rootViewController;
67  }
68  return nil;
69 }
70 
71 // Do not remove, some clients may be calling these via `super`.
72 - (void)applicationDidEnterBackground:(UIApplication*)application {
73 }
74 
75 // Do not remove, some clients may be calling these via `super`.
76 - (void)applicationWillEnterForeground:(UIApplication*)application {
77 }
78 
79 // Do not remove, some clients may be calling these via `super`.
80 - (void)applicationWillResignActive:(UIApplication*)application {
81 }
82 
83 // Do not remove, some clients may be calling these via `super`.
84 - (void)applicationDidBecomeActive:(UIApplication*)application {
85 }
86 
87 // Do not remove, some clients may be calling these via `super`.
88 - (void)applicationWillTerminate:(UIApplication*)application {
89 }
90 
91 #pragma GCC diagnostic push
92 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
93 - (void)application:(UIApplication*)application
94  didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings {
95  [self.lifeCycleDelegate application:application
96  didRegisterUserNotificationSettings:notificationSettings];
97 }
98 #pragma GCC diagnostic pop
99 
100 - (void)application:(UIApplication*)application
101  didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
102  [self.lifeCycleDelegate application:application
103  didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
104 }
105 
106 - (void)application:(UIApplication*)application
107  didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
108  [self.lifeCycleDelegate application:application
109  didFailToRegisterForRemoteNotificationsWithError:error];
110 }
111 
112 #pragma GCC diagnostic push
113 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
114 - (void)application:(UIApplication*)application
115  didReceiveLocalNotification:(UILocalNotification*)notification {
116  [self.lifeCycleDelegate application:application didReceiveLocalNotification:notification];
117 }
118 #pragma GCC diagnostic pop
119 
120 - (void)userNotificationCenter:(UNUserNotificationCenter*)center
121  willPresentNotification:(UNNotification*)notification
122  withCompletionHandler:
123  (void (^)(UNNotificationPresentationOptions options))completionHandler {
124  if ([self.lifeCycleDelegate respondsToSelector:_cmd]) {
125  [self.lifeCycleDelegate userNotificationCenter:center
126  willPresentNotification:notification
127  withCompletionHandler:completionHandler];
128  }
129 }
130 
131 /**
132  * Calls all plugins registered for `UNUserNotificationCenterDelegate` callbacks.
133  */
134 - (void)userNotificationCenter:(UNUserNotificationCenter*)center
135  didReceiveNotificationResponse:(UNNotificationResponse*)response
136  withCompletionHandler:(void (^)(void))completionHandler {
137  if ([self.lifeCycleDelegate respondsToSelector:_cmd]) {
138  [self.lifeCycleDelegate userNotificationCenter:center
139  didReceiveNotificationResponse:response
140  withCompletionHandler:completionHandler];
141  }
142 }
143 
144 // This method is called when opening an URL with custom schemes.
145 - (BOOL)application:(UIApplication*)application
146  openURL:(NSURL*)url
147  options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
148  if ([self.lifeCycleDelegate application:application openURL:url options:options]) {
149  return YES;
150  }
151 
152  return [self handleOpenURL:url options:options];
153 }
154 
155 // Helper function for opening an URL, either with a custom scheme or a http/https scheme.
156 - (BOOL)handleOpenURL:(NSURL*)url
157  options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
158  UIApplication* flutterApplication = FlutterSharedApplication.application;
159  if (flutterApplication == nil) {
160  return NO;
161  }
163  return NO;
164  }
165 
166  FlutterViewController* flutterViewController = [self rootFlutterViewController];
167  if (flutterViewController) {
168  [flutterViewController.engine sendDeepLinkToFramework:url
169  completionHandler:^(BOOL success){
170  // no-op.
171  }];
172  } else {
173  [FlutterLogger logError:@"Attempting to open an URL without a Flutter RootViewController."];
174  return NO;
175  }
176  return YES;
177 }
178 
179 - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
180  return [self.lifeCycleDelegate application:application handleOpenURL:url];
181 }
182 
183 - (BOOL)application:(UIApplication*)application
184  openURL:(NSURL*)url
185  sourceApplication:(NSString*)sourceApplication
186  annotation:(id)annotation {
187  return [self.lifeCycleDelegate application:application
188  openURL:url
189  sourceApplication:sourceApplication
190  annotation:annotation];
191 }
192 
193 - (void)application:(UIApplication*)application
194  performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
195  completionHandler:(void (^)(BOOL succeeded))completionHandler {
196  [self.lifeCycleDelegate application:application
197  performActionForShortcutItem:shortcutItem
198  completionHandler:completionHandler];
199 }
200 
201 - (void)application:(UIApplication*)application
202  handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
203  completionHandler:(nonnull void (^)())completionHandler {
204  [self.lifeCycleDelegate application:application
205  handleEventsForBackgroundURLSession:identifier
206  completionHandler:completionHandler];
207 }
208 
209 // This method is called when opening an URL with a http/https scheme.
210 - (BOOL)application:(UIApplication*)application
211  continueUserActivity:(NSUserActivity*)userActivity
212  restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>>* __nullable
213  restorableObjects))restorationHandler {
214  if ([self.lifeCycleDelegate application:application
215  continueUserActivity:userActivity
216  restorationHandler:restorationHandler]) {
217  return YES;
218  }
219 
220  return [self handleOpenURL:userActivity.webpageURL options:@{}];
221 }
222 
223 #pragma mark - FlutterPluginRegistry methods. All delegating to the rootViewController
224 
226  if (_weakRegistrant) {
227  return _weakRegistrant;
228  }
229  if (_strongRegistrant) {
230  return _strongRegistrant;
231  }
232  return nil;
233 }
234 
235 - (void)setPluginRegistrant:(NSObject<FlutterPluginRegistrant>*)pluginRegistrant {
236  if (pluginRegistrant == (id)self) {
237  _weakRegistrant = pluginRegistrant;
238  _strongRegistrant = nil;
239  } else {
240  _weakRegistrant = nil;
241  _strongRegistrant = pluginRegistrant;
242  }
243 }
244 
245 - (NSObject<FlutterPluginRegistrar>*)registrarForPlugin:(NSString*)pluginKey {
246  FlutterViewController* flutterRootViewController = [self rootFlutterViewController];
247  if (flutterRootViewController) {
248  return [[flutterRootViewController pluginRegistry] registrarForPlugin:pluginKey];
249  }
250  return [[self.launchEngine acquireEngine] registrarForPlugin:pluginKey];
251 }
252 
253 - (BOOL)hasPlugin:(NSString*)pluginKey {
254  FlutterViewController* flutterRootViewController = [self rootFlutterViewController];
255  if (flutterRootViewController) {
256  return [[flutterRootViewController pluginRegistry] hasPlugin:pluginKey];
257  }
258  return [[self.launchEngine acquireEngine] hasPlugin:pluginKey];
259 }
260 
261 - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey {
262  FlutterViewController* flutterRootViewController = [self rootFlutterViewController];
263  if (flutterRootViewController) {
264  return [[flutterRootViewController pluginRegistry] valuePublishedByPlugin:pluginKey];
265  }
266  return [[self.launchEngine acquireEngine] valuePublishedByPlugin:pluginKey];
267 }
268 
269 #pragma mark - Selectors handling
270 
271 - (void)addApplicationLifeCycleDelegate:(NSObject<FlutterApplicationLifeCycleDelegate>*)delegate {
272  [self.lifeCycleDelegate addDelegate:delegate];
273 }
274 
275 #pragma mark - UIApplicationDelegate method dynamic implementation
276 
277 - (BOOL)respondsToSelector:(SEL)selector {
278  if ([self.lifeCycleDelegate isSelectorAddedDynamically:selector]) {
279  return [self delegateRespondsSelectorToPlugins:selector];
280  }
281  return [super respondsToSelector:selector];
282 }
283 
284 - (BOOL)delegateRespondsSelectorToPlugins:(SEL)selector {
285  if ([self.lifeCycleDelegate hasPluginThatRespondsToSelector:selector]) {
286  return [self.lifeCycleDelegate respondsToSelector:selector];
287  } else {
288  return NO;
289  }
290 }
291 
292 - (id)forwardingTargetForSelector:(SEL)aSelector {
293  if ([self.lifeCycleDelegate isSelectorAddedDynamically:aSelector]) {
294  [self logCapabilityConfigurationWarningIfNeeded:aSelector];
295  return self.lifeCycleDelegate;
296  }
297  return [super forwardingTargetForSelector:aSelector];
298 }
299 
300 // Mimic the logging from Apple when the capability is not set for the selectors.
301 // However the difference is that Apple logs these message when the app launches, we only
302 // log it when the method is invoked. We can possibly also log it when the app launches, but
303 // it will cause an additional scan over all the plugins.
304 - (void)logCapabilityConfigurationWarningIfNeeded:(SEL)selector {
305  NSArray* backgroundModesArray =
306  [[NSBundle mainBundle] objectForInfoDictionaryKey:kUIBackgroundMode];
307  NSSet* backgroundModesSet = [[NSSet alloc] initWithArray:backgroundModesArray];
308  if (selector == @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)) {
309  if (![backgroundModesSet containsObject:kRemoteNotificationCapabitiliy]) {
310  NSLog(
311  @"You've implemented -[<UIApplicationDelegate> "
312  @"application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need "
313  @"to add \"remote-notification\" to the list of your supported UIBackgroundModes in your "
314  @"Info.plist.");
315  }
316  } else if (selector == @selector(application:performFetchWithCompletionHandler:)) {
317  if (![backgroundModesSet containsObject:kBackgroundFetchCapatibility]) {
318  NSLog(@"You've implemented -[<UIApplicationDelegate> "
319  @"application:performFetchWithCompletionHandler:], but you still need to add \"fetch\" "
320  @"to the list of your supported UIBackgroundModes in your Info.plist.");
321  }
322  }
323 }
324 
325 #pragma mark - State Restoration
326 
327 - (BOOL)application:(UIApplication*)application shouldSaveApplicationState:(NSCoder*)coder {
328  [coder encodeInt64:FlutterSharedApplication.lastAppModificationTime
329  forKey:kRestorationStateAppModificationKey];
330  return YES;
331 }
332 
333 - (BOOL)application:(UIApplication*)application shouldRestoreApplicationState:(NSCoder*)coder {
334  int64_t stateDate = [coder decodeInt64ForKey:kRestorationStateAppModificationKey];
336 }
337 
338 - (BOOL)application:(UIApplication*)application shouldSaveSecureApplicationState:(NSCoder*)coder {
339  [coder encodeInt64:FlutterSharedApplication.lastAppModificationTime
340  forKey:kRestorationStateAppModificationKey];
341  return YES;
342 }
343 
344 - (BOOL)application:(UIApplication*)application
345  shouldRestoreSecureApplicationState:(NSCoder*)coder {
346  int64_t stateDate = [coder decodeInt64ForKey:kRestorationStateAppModificationKey];
348 }
349 
350 @end
static NSString *const kRemoteNotificationCapabitiliy
static FLUTTER_ASSERT_ARC NSString *const kUIBackgroundMode
static NSString *const kBackgroundFetchCapatibility
__weak NSObject< FlutterPluginRegistrant > * _weakRegistrant
NSObject< FlutterPluginRegistrant > * _strongRegistrant
FlutterPluginAppLifeCycleDelegate * lifeCycleDelegate
NSObject< FlutterPluginRegistrant > * pluginRegistrant
id< FlutterPluginRegistry > pluginRegistry()