| 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
5 #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h"
6
7 #import "flutter/fml/logging.h"
8 #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h"
9 #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
10 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h"
11 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
12 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h"
13
14 static NSString* const kUIBackgroundMode = @"UIBackgroundModes";
15 static NSString* const kRemoteNotificationCapabitiliy = @"remote-notification";
16 static NSString* const kBackgroundFetchCapatibility = @"fetch";
17 static NSString* const kRestorationStateAppModificationKey = @"mod-date";
18
19 @interface FlutterAppDelegate ()
20 @property(nonatomic, copy) FlutterViewController* (^rootFlutterViewControllerGetter)(void);
21 @end
22
23 @implementation FlutterAppDelegate {
24 FlutterPluginAppLifeCycleDelegate* _lifeCycleDelegate;
25 }
26
27 - (instancetype)init {
28 if (self = [super init]) {
29 _lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
30 }
31 return self;
32 }
33
34 - (void)dealloc {
35 [_lifeCycleDelegate release];
36 [_rootFlutterViewControllerGetter release];
37 [_window release];
38 [super dealloc];
39 }
40
41 - (BOOL)application:(UIApplication*)application
42 willFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
43 return [_lifeCycleDelegate application:application willFinishLaunchingWithOptions:launchOptions];
44 }
45
46 - (BOOL)application:(UIApplication*)application
47 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
48 return [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions];
49 }
50
51 // Returns the key window's rootViewController, if it's a FlutterViewController.
52 // Otherwise, returns nil.
53 - (FlutterViewController*)rootFlutterViewController {
54 if (_rootFlutterViewControllerGetter != nil) {
55 return _rootFlutterViewControllerGetter();
56 }
57 UIViewController* rootViewController = _window.rootViewController;
58 if ([rootViewController isKindOfClass:[FlutterViewController class]]) {
59 return (FlutterViewController*)rootViewController;
60 }
61 return nil;
62 }
63
64 // Do not remove, some clients may be calling these via `super`.
65 - (void)applicationDidEnterBackground:(UIApplication*)application {
66 }
67
68 // Do not remove, some clients may be calling these via `super`.
69 - (void)applicationWillEnterForeground:(UIApplication*)application {
70 }
71
72 // Do not remove, some clients may be calling these via `super`.
73 - (void)applicationWillResignActive:(UIApplication*)application {
74 }
75
76 // Do not remove, some clients may be calling these via `super`.
77 - (void)applicationDidBecomeActive:(UIApplication*)application {
78 }
79
80 // Do not remove, some clients may be calling these via `super`.
81 - (void)applicationWillTerminate:(UIApplication*)application {
82 }
83
84 #pragma GCC diagnostic push
85 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
86 - (void)application:(UIApplication*)application
87 didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings {
88 [_lifeCycleDelegate application:application
89 didRegisterUserNotificationSettings:notificationSettings];
90 }
91 #pragma GCC diagnostic pop
92
93 - (void)application:(UIApplication*)application
94 didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
95 [_lifeCycleDelegate application:application
96 didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
97 }
98
99 - (void)application:(UIApplication*)application
100 didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
101 [_lifeCycleDelegate application:application
102 didFailToRegisterForRemoteNotificationsWithError:error];
103 }
104
105 #pragma GCC diagnostic push
106 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
107 - (void)application:(UIApplication*)application
108 didReceiveLocalNotification:(UILocalNotification*)notification {
109 [_lifeCycleDelegate application:application didReceiveLocalNotification:notification];
110 }
111 #pragma GCC diagnostic pop
112
113 - (void)userNotificationCenter:(UNUserNotificationCenter*)center
114 willPresentNotification:(UNNotification*)notification
115 withCompletionHandler:
116 (void (^)(UNNotificationPresentationOptions options))completionHandler {
117 if ([_lifeCycleDelegate respondsToSelector:_cmd]) {
118 [_lifeCycleDelegate userNotificationCenter:center
119 willPresentNotification:notification
120 withCompletionHandler:completionHandler];
121 }
122 }
123
124 /**
125 * Calls all plugins registered for `UNUserNotificationCenterDelegate` callbacks.
126 */
127 - (void)userNotificationCenter:(UNUserNotificationCenter*)center
128 didReceiveNotificationResponse:(UNNotificationResponse*)response
129 withCompletionHandler:(void (^)(void))completionHandler {
130 if ([_lifeCycleDelegate respondsToSelector:_cmd]) {
131 [_lifeCycleDelegate userNotificationCenter:center
132 didReceiveNotificationResponse:response
133 withCompletionHandler:completionHandler];
134 }
135 }
136
137 - (BOOL)openURL:(NSURL*)url {
138 NSNumber* isDeepLinkingEnabled =
139 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FlutterDeepLinkingEnabled"];
140 if (!isDeepLinkingEnabled.boolValue) {
141 // Not set or NO.
142 return NO;
143 } else {
144 FlutterViewController* flutterViewController = [self rootFlutterViewController];
145 if (flutterViewController) {
146 [flutterViewController.engine
147 waitForFirstFrame:3.0
148 callback:^(BOOL didTimeout) {
149 if (didTimeout) {
150 FML_LOG(ERROR)
151 << "Timeout waiting for the first frame when launching an URL.";
152 } else {
153 [flutterViewController.engine.navigationChannel
154 invokeMethod:@"pushRouteInformation"
155 arguments:@{
156 @"location" : url.absoluteString ?: [NSNull null],
157 }];
158 }
159 }];
160 return YES;
161 } else {
162 FML_LOG(ERROR) << "Attempting to open an URL without a Flutter RootViewController.";
163 return NO;
164 }
165 }
166 }
167
168 - (BOOL)application:(UIApplication*)application
169 openURL:(NSURL*)url
170 options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
171 if ([_lifeCycleDelegate application:application openURL:url options:options]) {
172 return YES;
173 }
174 return [self openURL:url];
175 }
176
177 - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
178 return [_lifeCycleDelegate application:application handleOpenURL:url];
179 }
180
181 - (BOOL)application:(UIApplication*)application
182 openURL:(NSURL*)url
183 sourceApplication:(NSString*)sourceApplication
184 annotation:(id)annotation {
185 return [_lifeCycleDelegate application:application
186 openURL:url
187 sourceApplication:sourceApplication
188 annotation:annotation];
189 }
190
191 - (void)application:(UIApplication*)application
192 performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
193 completionHandler:(void (^)(BOOL succeeded))completionHandler {
194 [_lifeCycleDelegate application:application
195 performActionForShortcutItem:shortcutItem
196 completionHandler:completionHandler];
197 }
198
199 - (void)application:(UIApplication*)application
200 handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
201 completionHandler:(nonnull void (^)())completionHandler {
202 [_lifeCycleDelegate application:application
203 handleEventsForBackgroundURLSession:identifier
204 completionHandler:completionHandler];
205 }
206
207 - (BOOL)application:(UIApplication*)application
208 continueUserActivity:(NSUserActivity*)userActivity
209 restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>>* __nullable
210 restorableObjects))restorationHandler {
211 if ([_lifeCycleDelegate application:application
212 continueUserActivity:userActivity
213 restorationHandler:restorationHandler]) {
214 return YES;
215 }
216 return [self openURL:userActivity.webpageURL];
217 }
218
219 #pragma mark - FlutterPluginRegistry methods. All delegating to the rootViewController
220
221 - (NSObject<FlutterPluginRegistrar>*)registrarForPlugin:(NSString*)pluginKey {
222 FlutterViewController* flutterRootViewController = [self rootFlutterViewController];
223 if (flutterRootViewController) {
224 return [[flutterRootViewController pluginRegistry] registrarForPlugin:pluginKey];
225 }
226 return nil;
227 }
228
229 - (BOOL)hasPlugin:(NSString*)pluginKey {
230 FlutterViewController* flutterRootViewController = [self rootFlutterViewController];
231 if (flutterRootViewController) {
232 return [[flutterRootViewController pluginRegistry] hasPlugin:pluginKey];
233 }
234 return false;
235 }
236
237 - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey {
238 FlutterViewController* flutterRootViewController = [self rootFlutterViewController];
239 if (flutterRootViewController) {
240 return [[flutterRootViewController pluginRegistry] valuePublishedByPlugin:pluginKey];
241 }
242 return nil;
243 }
244
245 #pragma mark - Selectors handling
246
247 - (void)addApplicationLifeCycleDelegate:(NSObject<FlutterApplicationLifeCycleDelegate>*)delegate {
248 [_lifeCycleDelegate addDelegate:delegate];
249 }
250
251 #pragma mark - UIApplicationDelegate method dynamic implementation
252
253 - (BOOL)respondsToSelector:(SEL)selector {
254 if ([_lifeCycleDelegate isSelectorAddedDynamically:selector]) {
255 return [self delegateRespondsSelectorToPlugins:selector];
256 }
257 return [super respondsToSelector:selector];
258 }
259
260 - (BOOL)delegateRespondsSelectorToPlugins:(SEL)selector {
261 if ([_lifeCycleDelegate hasPluginThatRespondsToSelector:selector]) {
262 return [_lifeCycleDelegate respondsToSelector:selector];
263 } else {
264 return NO;
265 }
266 }
267
268 - (id)forwardingTargetForSelector:(SEL)aSelector {
269 if ([_lifeCycleDelegate isSelectorAddedDynamically:aSelector]) {
270 [self logCapabilityConfigurationWarningIfNeeded:aSelector];
271 return _lifeCycleDelegate;
272 }
273 return [super forwardingTargetForSelector:aSelector];
274 }
275
276 // Mimic the logging from Apple when the capability is not set for the selectors.
277 // However the difference is that Apple logs these message when the app launches, we only
278 // log it when the method is invoked. We can possibly also log it when the app launches, but
279 // it will cause an additional scan over all the plugins.
280 - (void)logCapabilityConfigurationWarningIfNeeded:(SEL)selector {
281 NSArray* backgroundModesArray =
282 [[NSBundle mainBundle] objectForInfoDictionaryKey:kUIBackgroundMode];
283 NSSet* backgroundModesSet = [[[NSSet alloc] initWithArray:backgroundModesArray] autorelease];
284 if (selector == @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)) {
285 if (![backgroundModesSet containsObject:kRemoteNotificationCapabitiliy]) {
286 NSLog(
287 @"You've implemented -[<UIApplicationDelegate> "
288 @"application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need "
289 @"to add \"remote-notification\" to the list of your supported UIBackgroundModes in your "
290 @"Info.plist.");
291 }
292 } else if (selector == @selector(application:performFetchWithCompletionHandler:)) {
293 if (![backgroundModesSet containsObject:kBackgroundFetchCapatibility]) {
294 NSLog(@"You've implemented -[<UIApplicationDelegate> "
295 @"application:performFetchWithCompletionHandler:], but you still need to add \"fetch\" "
296 @"to the list of your supported UIBackgroundModes in your Info.plist.");
297 }
298 }
299 }
300
301 #pragma mark - State Restoration
302
303 - (BOOL)application:(UIApplication*)application shouldSaveApplicationState:(NSCoder*)coder {
304 [coder encodeInt64:self.lastAppModificationTime forKey:kRestorationStateAppModificationKey];
305 return YES;
306 }
307
308 - (BOOL)application:(UIApplication*)application shouldRestoreApplicationState:(NSCoder*)coder {
309 int64_t stateDate = [coder decodeInt64ForKey:kRestorationStateAppModificationKey];
310 return self.lastAppModificationTime == stateDate;
311 }
312
313 - (BOOL)application:(UIApplication*)application shouldSaveSecureApplicationState:(NSCoder*)coder {
314 [coder encodeInt64:self.lastAppModificationTime forKey:kRestorationStateAppModificationKey];
315 return YES;
316 }
317
318 - (BOOL)application:(UIApplication*)application
319 shouldRestoreSecureApplicationState:(NSCoder*)coder {
320 int64_t stateDate = [coder decodeInt64ForKey:kRestorationStateAppModificationKey];
321 return self.lastAppModificationTime == stateDate;
322 }
323
324 - (int64_t)lastAppModificationTime {
325 NSDate* fileDate;
326 NSError* error = nil;
327 [[[NSBundle mainBundle] executableURL] getResourceValue:&fileDate
328 forKey:NSURLContentModificationDateKey
329 error:&error];
330 NSAssert(error == nil, @"Cannot obtain modification date of main bundle: %@", error);
331 return [fileDate timeIntervalSince1970];
332 }
333
334 @end
FlutterViewController
Definition: FlutterViewController.h:56
kRestorationStateAppModificationKey
static NSString *const kRestorationStateAppModificationKey
Definition: FlutterAppDelegate.mm:17
FlutterEngine_Internal.h
FlutterPluginAppLifeCycleDelegate.h
FlutterPluginRegistrar-p
Definition: FlutterPlugin.h:283
FlutterPluginAppLifeCycleDelegate
Definition: FlutterPluginAppLifeCycleDelegate.h:16
kUIBackgroundMode
static NSString *const kUIBackgroundMode
Definition: FlutterAppDelegate.mm:14
FlutterApplicationLifeCycleDelegate-p
Definition: FlutterPlugin.h:25
FlutterAppDelegate
Definition: FlutterAppDelegate.h:27
FlutterPluginAppLifeCycleDelegate_internal.h
FlutterAppDelegate.h
-[FlutterViewController pluginRegistry]
id< FlutterPluginRegistry > pluginRegistry()
Definition: FlutterViewController.mm:2348
FlutterAppDelegate(Test)::rootFlutterViewControllerGetter
FlutterViewController *(^ rootFlutterViewControllerGetter)(void)
FlutterAppDelegate_Test.h
kBackgroundFetchCapatibility
static NSString *const kBackgroundFetchCapatibility
Definition: FlutterAppDelegate.mm:16
FlutterViewController.h
kRemoteNotificationCapabitiliy
static NSString *const kRemoteNotificationCapabitiliy
Definition: FlutterAppDelegate.mm:15