From 1020a201ca47b1659c5e0d3a53c9a9b747878a8d Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Thu, 30 May 2019 09:06:49 -0700 Subject: [PATCH] Dynamically add certain iOS AppDelegate methods. (flutter/engine#8843) --- .../ci/licenses_golden/licenses_flutter | 1 + .../FlutterPluginAppLifeCycleDelegate.h | 1 + .../framework/Source/FlutterAppDelegate.mm | 71 +++++++++++++++---- .../FlutterPluginAppLifeCycleDelegate.mm | 25 +++++++ ...utterPluginAppLifeCycleDelegate_internal.h | 18 +++++ 5 files changed, 102 insertions(+), 14 deletions(-) create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 0cf03df555..81d14111ac 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -677,6 +677,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatfor FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec_Internal.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h index a8dda282b5..d593606b6f 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h @@ -14,6 +14,7 @@ NS_ASSUME_NONNULL_BEGIN */ FLUTTER_EXPORT @interface FlutterPluginAppLifeCycleDelegate : NSObject + /** * Registers `delegate` to receive life cycle callbacks via this FlutterPluginAppLifecycleDelegate * as long as it is alive. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm index f3f1786e45..f0dd78e111 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -3,8 +3,14 @@ // found in the LICENSE file. #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h" +#include "flutter/fml/logging.h" #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h" #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" +#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h" + +static NSString* kUIBackgroundMode = @"UIBackgroundModes"; +static NSString* kRemoteNotificationCapabitiliy = @"remote-notification"; +static NSString* kBackgroundFetchCapatibility = @"fetch"; @implementation FlutterAppDelegate { FlutterPluginAppLifeCycleDelegate* _lifeCycleDelegate; @@ -86,14 +92,6 @@ didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } -- (void)application:(UIApplication*)application - didReceiveRemoteNotification:(NSDictionary*)userInfo - fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { - [_lifeCycleDelegate application:application - didReceiveRemoteNotification:userInfo - fetchCompletionHandler:completionHandler]; -} - - (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification { [_lifeCycleDelegate application:application didReceiveLocalNotification:notification]; @@ -147,11 +145,6 @@ completionHandler:completionHandler]; } -- (void)application:(UIApplication*)application - performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { - [_lifeCycleDelegate application:application performFetchWithCompletionHandler:completionHandler]; -} - #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000 - (BOOL)application:(UIApplication*)application continueUserActivity:(NSUserActivity*)userActivity @@ -195,10 +188,60 @@ return nil; } -#pragma mark - FlutterAppLifeCycleProvider methods +#pragma mark - Selectors handling - (void)addApplicationLifeCycleDelegate:(NSObject*)delegate { [_lifeCycleDelegate addDelegate:delegate]; } +#pragma mark - UIApplicationDelegate method dynamic implementation + +- (BOOL)respondsToSelector:(SEL)selector { + if ([_lifeCycleDelegate isSelectorAddedDynamically:selector]) { + return [self delegateRespondsSelectorToPlugins:selector]; + } + return [super respondsToSelector:selector]; +} + +- (BOOL)delegateRespondsSelectorToPlugins:(SEL)selector { + if ([_lifeCycleDelegate hasPluginThatRespondsToSelector:selector]) { + return [_lifeCycleDelegate respondsToSelector:selector]; + } else { + return NO; + } +} + +- (id)forwardingTargetForSelector:(SEL)aSelector { + if ([_lifeCycleDelegate isSelectorAddedDynamically:aSelector]) { + [self logCapabilityConfigurationWarningIfNeeded:aSelector]; + return _lifeCycleDelegate; + } + return [super forwardingTargetForSelector:aSelector]; +} + +// Mimic the logging from Apple when the capability is not set for the selectors. +// However the difference is that Apple logs these message when the app launches, we only +// log it when the method is invoked. We can possibly also log it when the app launches, but +// it will cause an additional scan over all the plugins. +- (void)logCapabilityConfigurationWarningIfNeeded:(SEL)selector { + NSArray* backgroundModesArray = + [[NSBundle mainBundle] objectForInfoDictionaryKey:kUIBackgroundMode]; + NSSet* backgroundModesSet = [[[NSSet alloc] initWithArray:backgroundModesArray] autorelease]; + if (selector == @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)) { + if (![backgroundModesSet containsObject:kRemoteNotificationCapabitiliy]) { + NSLog( + @"You've implemented -[ " + @"application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need " + @"to add \"remote-notification\" to the list of your supported UIBackgroundModes in your " + @"Info.plist."); + } + } else if (selector == @selector(application:performFetchWithCompletionHandler:)) { + if (![backgroundModesSet containsObject:kBackgroundFetchCapatibility]) { + NSLog(@"You've implemented -[ " + @"application:performFetchWithCompletionHandler:], but you still need to add \"fetch\" " + @"to the list of your supported UIBackgroundModes in your Info.plist."); + } + } +} + @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm index 054e8b5556..afeff246fc 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm @@ -11,6 +11,10 @@ static const char* kCallbackCacheSubDir = "Library/Caches/"; +static const SEL selectorsHandledByPlugins[] = { + @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:), + @selector(application:performFetchWithCompletionHandler:)}; + @implementation FlutterPluginAppLifeCycleDelegate { UIBackgroundTaskIdentifier _debugBackgroundTask; @@ -36,6 +40,27 @@ static BOOL isPowerOfTwo(NSUInteger x) { return x != 0 && (x & (x - 1)) == 0; } +- (BOOL)isSelectorAddedDynamically:(SEL)selector { + for (const SEL& aSelector : selectorsHandledByPlugins) { + if (selector == aSelector) { + return YES; + } + } + return NO; +} + +- (BOOL)hasPluginThatRespondsToSelector:(SEL)selector { + for (id plugin in [_pluginDelegates allObjects]) { + if (!plugin) { + continue; + } + if ([plugin respondsToSelector:selector]) { + return YES; + } + } + return NO; +} + - (void)addDelegate:(NSObject*)delegate { [_pluginDelegates addPointer:(__bridge void*)delegate]; if (isPowerOfTwo([_pluginDelegates count])) { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h new file mode 100644 index 0000000000..00e8696199 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h @@ -0,0 +1,18 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +@interface FlutterPluginAppLifeCycleDelegate () + +/** + * Check whether the selector should be handled dynamically. + */ +- (BOOL)isSelectorAddedDynamically:(SEL)selector; + +/** + * Check whether there is at least one plugin responds to the selector. + */ +- (BOOL)hasPluginThatRespondsToSelector:(SEL)selector; + +@end +;