diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index c80eae1737..3e71549dea 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -23,6 +23,7 @@ shared_library("flutter_framework_dylib") { "framework/Headers/FlutterCodecs.h", "framework/Headers/FlutterDartProject.h", "framework/Headers/FlutterMacros.h", + "framework/Headers/FlutterPlugin.h", "framework/Headers/FlutterViewController.h", "framework/Source/FlutterAppDelegate.mm", "framework/Source/FlutterChannels.mm", @@ -163,6 +164,7 @@ copy("framework_headers") { "framework/Headers/FlutterCodecs.h", "framework/Headers/FlutterDartProject.h", "framework/Headers/FlutterMacros.h", + "framework/Headers/FlutterPlugin.h", "framework/Headers/FlutterViewController.h", ] outputs = [ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h index e75c38f27f..8501f6188b 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h @@ -11,6 +11,7 @@ #include "FlutterCodecs.h" #include "FlutterDartProject.h" #include "FlutterMacros.h" +#include "FlutterPlugin.h" #include "FlutterViewController.h" #endif // FLUTTER_FLUTTER_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h index 90a4f344ed..1e9066356e 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h @@ -8,6 +8,7 @@ #import #include "FlutterMacros.h" +#include "FlutterPlugin.h" /** * UIApplicationDelegate subclass for simple apps that want default behavior. @@ -23,7 +24,8 @@ * code as necessary from FlutterAppDelegate.mm. */ FLUTTER_EXPORT -@interface FlutterAppDelegate : UIResponder +@interface FlutterAppDelegate + : UIResponder @property(strong, nonatomic) UIWindow* window; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h new file mode 100644 index 0000000000..03ffa57205 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h @@ -0,0 +1,55 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_FLUTTERPLUGIN_H_ +#define FLUTTER_FLUTTERPLUGIN_H_ + +#include "FlutterBinaryMessenger.h" +#include "FlutterChannels.h" +#include "FlutterCodecs.h" + +NS_ASSUME_NONNULL_BEGIN +@protocol FlutterPluginRegistrar; + +@protocol FlutterPlugin +@required ++ (void)registerWithRegistrar:(NSObject*)registrar; +@optional +- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; +- (BOOL)application:(UIApplication*)application + didFinishLaunchingWithOptions:(NSDictionary*)launchOptions; +- (void)applicationDidBecomeActive:(UIApplication*)application; +- (void)applicationWillResignActive:(UIApplication*)application; +- (void)applicationDidEnterBackground:(UIApplication*)application; +- (void)applicationWillEnterForeground:(UIApplication*)application; +- (void)applicationWillTerminate:(UIApplication*)application; +- (void)application:(UIApplication*)application + didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings; +- (void)application:(UIApplication*)application + didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken; +- (BOOL)application:(UIApplication*)application + didReceiveRemoteNotification:(NSDictionary*)userInfo + fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler; +- (BOOL)application:(UIApplication*)application + openURL:(NSURL*)url + options:(NSDictionary*)options; +@end + +@protocol FlutterPluginRegistrar +- (NSObject*)messenger; +- (void)publish:(NSObject*)value; +- (void)addMethodCallDelegate:(NSObject*)delegate + channel:(FlutterMethodChannel*)channel; +- (void)addApplicationDelegate:(NSObject*)delegate; +@end + +@protocol FlutterPluginRegistry +- (NSObject*)registrarForPlugin:(NSString*)pluginKey; +- (BOOL)hasPlugin:(NSString*)pluginKey; +- (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey; +@end + +NS_ASSUME_NONNULL_END; + +#endif // FLUTTER_FLUTTERPLUGIN_H_ 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 50627c8e3e..cb72aed21a 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 @@ -6,10 +6,45 @@ #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" #include "lib/ftl/logging.h" +@interface FlutterAppDelegate () +@property(readonly, nonatomic) NSMutableArray* pluginDelegates; +@property(readonly, nonatomic) NSMutableDictionary* pluginPublications; +@end + +@interface FlutterAppDelegateRegistrar : NSObject +- (instancetype)initWithPlugin:(NSString*)pluginKey appDelegate:(FlutterAppDelegate*)delegate; +@end + @implementation FlutterAppDelegate { UIBackgroundTaskIdentifier _debugBackgroundTask; } +- (instancetype)init { + if (self = [super init]) { + _pluginDelegates = [NSMutableArray new]; + _pluginPublications = [NSMutableDictionary new]; + } + return self; +} + +- (void)dealloc { + [_pluginDelegates release]; + [_pluginPublications release]; + [super dealloc]; +} + +- (BOOL)application:(UIApplication*)application + didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + if (![plugin application:application didFinishLaunchingWithOptions:launchOptions]) { + return NO; + } + } + } + return YES; +} + // Returns the key window's rootViewController, if it's a FlutterViewController. // Otherwise, returns nil. - (FlutterViewController*)rootFlutterViewController { @@ -29,25 +64,161 @@ } } +- (void)applicationDidEnterBackground:(UIApplication*)application { #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG -- (void)applicationDidEnterBackground:(UIApplication *)application { // The following keeps the Flutter session alive when the device screen locks - // in debug mode. It allows continued use of features like hot reload and + // in debug mode. It allows continued use of features like hot reload and // taking screenshots once the device unlocks again. // - // Note the name is not an identifier and multiple instances can exist. - _debugBackgroundTask = [application beginBackgroundTaskWithName:@"Flutter debug task" - expirationHandler:^{ - FTL_LOG(WARNING) << "\nThe OS has terminated the Flutter debug connection for being " - "inactive in the background for too long.\n\n" - "There are no errors with your Flutter application.\n\n" - "To reconnect, launch your application again via 'flutter run"; - }]; -} - -- (void)applicationWillEnterForeground:(UIApplication *)application { - [application endBackgroundTask: _debugBackgroundTask]; -} + // Note the name is not an identifier and multiple instances can exist. + _debugBackgroundTask = [application + beginBackgroundTaskWithName:@"Flutter debug task" + expirationHandler:^{ + FTL_LOG(WARNING) + << "\nThe OS has terminated the Flutter debug connection for being " + "inactive in the background for too long.\n\n" + "There are no errors with your Flutter application.\n\n" + "To reconnect, launch your application again via 'flutter run"; + }]; #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + [plugin applicationDidEnterBackground:application]; + } + } +} +- (void)applicationWillEnterForeground:(UIApplication*)application { +#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG + [application endBackgroundTask:_debugBackgroundTask]; +#endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + [plugin applicationWillEnterForeground:application]; + } + } +} + +- (void)applicationWillResignActive:(UIApplication*)application { + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + [plugin applicationWillResignActive:application]; + } + } +} + +- (void)applicationDidBecomeActive:(UIApplication*)application { + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + [plugin applicationDidBecomeActive:application]; + } + } +} + +- (void)applicationWillTerminate:(UIApplication*)application { + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + [plugin applicationWillTerminate:application]; + } + } +} + +- (void)application:(UIApplication*)application + didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings { + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + [plugin application:application didRegisterUserNotificationSettings:notificationSettings]; + } + } +} + +- (void)application:(UIApplication*)application + didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + [plugin application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; + } + } +} + +- (void)application:(UIApplication*)application + didReceiveRemoteNotification:(NSDictionary*)userInfo + fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + if ([plugin application:application + didReceiveRemoteNotification:userInfo + fetchCompletionHandler:completionHandler]) { + return; + } + } + } +} + +- (BOOL)application:(UIApplication*)application + openURL:(NSURL*)url + options:(NSDictionary*)options { + for (id plugin in _pluginDelegates) { + if ([plugin respondsToSelector:_cmd]) { + if ([plugin application:application openURL:url options:options]) { + return YES; + } + } + } + return NO; +} + +- (NSObject*)registrarForPlugin:(NSString*)pluginKey { + NSAssert(self.pluginPublications[pluginKey] == nil, @"Duplicate plugin key: %@", pluginKey); + self.pluginPublications[pluginKey] = [NSNull null]; + return + [[[FlutterAppDelegateRegistrar alloc] initWithPlugin:pluginKey appDelegate:self] autorelease]; +} + +- (BOOL)hasPlugin:(NSString*)pluginKey { + return _pluginPublications[pluginKey] != nil; +} + +- (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey { + return _pluginPublications[pluginKey]; +} +@end + +@implementation FlutterAppDelegateRegistrar { + NSString* _pluginKey; + FlutterAppDelegate* _appDelegate; +} + +- (instancetype)initWithPlugin:(NSString*)pluginKey appDelegate:(FlutterAppDelegate*)appDelegate { + self = [super init]; + NSAssert(self, @"Super init cannot be nil"); + _pluginKey = [pluginKey retain]; + _appDelegate = [appDelegate retain]; + return self; +} + +- (void)dealloc { + [_pluginKey release]; + [_appDelegate release]; + [super dealloc]; +} + +- (NSObject*)messenger { + return (FlutterViewController*)_appDelegate.window.rootViewController; +} + +- (void)publish:(NSObject*)value { + _appDelegate.pluginPublications[_pluginKey] = value; +} + +- (void)addMethodCallDelegate:(NSObject*)delegate + channel:(FlutterMethodChannel*)channel { + [channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { + [delegate handleMethodCall:call result:result]; + }]; +} + +- (void)addApplicationDelegate:(NSObject*)delegate { + [_appDelegate.pluginDelegates addObject:delegate]; +} @end