Skip to content

Commit 597fa9e

Browse files
authored
Delegate AppDelegate life-cycle callbacks to plugins via separate object (#5173)
* Move the handling of delegating AppDelegate callback out of FlutterAppDelegate. Also moves the plugin registry to FlutterViewController. So each view-controller will handle its own plugins. This is intended to simplify including one or more Flutter views in an existing iOS app and giving more precise control of plugin registration. Fixes: #16539 * formatting * Update license golden file
1 parent ef0915c commit 597fa9e

10 files changed

Lines changed: 566 additions & 219 deletions

File tree

‎shell/platform/darwin/ios/BUILD.gn‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ _flutter_framework_headers = [
2222
"framework/Headers/FlutterMacros.h",
2323
"framework/Headers/FlutterNavigationController.h",
2424
"framework/Headers/FlutterPlugin.h",
25+
"framework/Headers/FlutterPluginAppLifeCycleDelegate.h",
2526
"framework/Headers/FlutterTexture.h",
2627
"framework/Headers/FlutterViewController.h",
2728
]
@@ -43,6 +44,7 @@ shared_library("create_flutter_framework_dylib") {
4344
"framework/Source/FlutterNavigationController.mm",
4445
"framework/Source/FlutterPlatformPlugin.h",
4546
"framework/Source/FlutterPlatformPlugin.mm",
47+
"framework/Source/FlutterPluginAppLifeCycleDelegate.mm",
4648
"framework/Source/FlutterStandardCodec.mm",
4749
"framework/Source/FlutterStandardCodec_Internal.h",
4850
"framework/Source/FlutterTextInputDelegate.h",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "FlutterMacros.h"
4444
#include "FlutterNavigationController.h"
4545
#include "FlutterPlugin.h"
46+
#include "FlutterPluginAppLifeCycleDelegate.h"
4647
#include "FlutterTexture.h"
4748
#include "FlutterViewController.h"
4849

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,10 @@
2424
* code as necessary from FlutterAppDelegate.mm.
2525
*/
2626
FLUTTER_EXPORT
27-
@interface FlutterAppDelegate : UIResponder<UIApplicationDelegate, FlutterPluginRegistry>
27+
@interface FlutterAppDelegate : UIResponder<UIApplicationDelegate, FlutterPluginRegistry, FlutterAppLifeCycleProvider>
2828

2929
@property(strong, nonatomic) UIWindow* window;
3030

31-
// Can be overriden by subclasses to provide a custom FlutterBinaryMessenger,
32-
// typically a FlutterViewController, for plugin interop.
33-
//
34-
// Defaults to window's rootViewController.
35-
- (NSObject<FlutterBinaryMessenger>*)binaryMessenger;
36-
37-
// Can be overriden by subclasses to provide a custom FlutterTextureRegistry,
38-
// typically a FlutterViewController, for plugin interop.
39-
//
40-
// Defaults to window's rootViewController.
41-
- (NSObject<FlutterTextureRegistry>*)textures;
42-
4331
@end
4432

4533
#endif // FLUTTER_FLUTTERDARTPROJECT_H_

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ NS_ASSUME_NONNULL_BEGIN
253253
- (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey;
254254
@end
255255

256+
/**
257+
Implement this in the `UIAppDelegate` of your app to enable Flutter plugins to register themselves to the application
258+
life cycle events.
259+
*/
260+
@protocol FlutterAppLifeCycleProvider
261+
- (void)addApplicationLifeCycleDelegate:(NSObject<FlutterPlugin>*)delegate;
262+
@end
263+
256264
NS_ASSUME_NONNULL_END;
257265

258266
#endif // FLUTTER_FLUTTERPLUGIN_H_
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright 2018 The Chromium 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+
#ifndef FLUTTER_FLUTTERPLUGINAPPLIFECYCLEDELEGATE_H_
6+
#define FLUTTER_FLUTTERPLUGINAPPLIFECYCLEDELEGATE_H_
7+
8+
#include "FlutterPlugin.h"
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
/**
13+
Propagates `UIAppDelegate` callbacks to registered plugins.
14+
*/
15+
FLUTTER_EXPORT
16+
@interface FlutterPluginAppLifeCycleDelegate : NSObject
17+
/**
18+
Registers `delegate` to receive life cycle callbacks via this FlutterPluginAppLifecycleDelegate as long as it is alive.
19+
20+
`delegate` will only referenced weakly.
21+
*/
22+
- (void)addDelegate:(NSObject<FlutterPlugin>*)delegate;
23+
24+
/**
25+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
26+
27+
- Returns: `NO` if any plugin vetoes application launch.
28+
*/
29+
- (BOOL)application:(UIApplication*)application
30+
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions;
31+
32+
/**
33+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
34+
*/
35+
- (void)applicationDidBecomeActive:(UIApplication*)application;
36+
37+
/**
38+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
39+
*/
40+
- (void)applicationWillResignActive:(UIApplication*)application;
41+
42+
/**
43+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
44+
*/
45+
- (void)applicationDidEnterBackground:(UIApplication*)application;
46+
47+
/**
48+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
49+
*/
50+
- (void)applicationWillEnterForeground:(UIApplication*)application;
51+
52+
/**
53+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
54+
*/
55+
- (void)applicationWillTerminate:(UIApplication*)application;
56+
57+
/**
58+
Called if this plugin has been registered for `UIApplicationDelegate` callbacks.
59+
*/
60+
- (void)application:(UIApplication*)application
61+
didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings;
62+
63+
/**
64+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
65+
*/
66+
- (void)application:(UIApplication*)application
67+
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken;
68+
69+
/**
70+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
71+
*/
72+
- (void)application:(UIApplication*)application
73+
didReceiveRemoteNotification:(NSDictionary*)userInfo
74+
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
75+
76+
/**
77+
Calls all plugins registered for `UIApplicationDelegate` callbacks in order of registration until some plugin handles
78+
the request.
79+
80+
- Returns: `YES` if any plugin handles the request.
81+
*/
82+
- (BOOL)application:(UIApplication*)application
83+
openURL:(NSURL*)url
84+
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options;
85+
86+
/**
87+
Calls all plugins registered for `UIApplicationDelegate` callbacks in order of registration until some plugin handles
88+
the request.
89+
90+
- Returns: `YES` if any plugin handles the request.
91+
*/
92+
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url;
93+
94+
/**
95+
Calls all plugins registered for `UIApplicationDelegate` callbacks in order of registration until some plugin handles
96+
the request.
97+
98+
- Returns: `YES` if any plugin handles the request.
99+
*/
100+
- (BOOL)application:(UIApplication*)application
101+
openURL:(NSURL*)url
102+
sourceApplication:(NSString*)sourceApplication
103+
annotation:(id)annotation;
104+
105+
/**
106+
Calls all plugins registered for `UIApplicationDelegate` callbacks.
107+
*/
108+
- (void)application:(UIApplication*)application
109+
performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
110+
completionHandler:(void (^)(BOOL succeeded))completionHandler
111+
API_AVAILABLE(ios(9.0));
112+
113+
/**
114+
Calls all plugins registered for `UIApplicationDelegate` callbacks in order of registration until some plugin handles
115+
the request.
116+
117+
- Returns: `YES` if any plugin handles the request.
118+
*/
119+
- (BOOL)application:(UIApplication*)application
120+
handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
121+
completionHandler:(nonnull void (^)())completionHandler;
122+
123+
/**
124+
Calls all plugins registered for `UIApplicationDelegate` callbacks in order of registration until some plugin handles
125+
the request.
126+
127+
- Returns: `YES` if any plugin handles the request.
128+
*/
129+
- (BOOL)application:(UIApplication*)application
130+
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
131+
@end
132+
133+
NS_ASSUME_NONNULL_END
134+
135+
#endif // FLUTTER_FLUTTERPLUGINAPPLIFECYCLEDELEGATE_H_

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
#include "FlutterBinaryMessenger.h"
1212
#include "FlutterDartProject.h"
1313
#include "FlutterMacros.h"
14+
#include "FlutterPlugin.h"
1415
#include "FlutterTexture.h"
1516

1617
FLUTTER_EXPORT
17-
@interface FlutterViewController : UIViewController<FlutterBinaryMessenger, FlutterTextureRegistry>
18+
@interface FlutterViewController : UIViewController<FlutterBinaryMessenger, FlutterTextureRegistry, FlutterPluginRegistry>
1819

1920
- (instancetype)initWithProject:(FlutterDartProject*)project
2021
nibName:(NSString*)nibNameOrNil
@@ -49,6 +50,8 @@ FLUTTER_EXPORT
4950
*/
5051
- (void)setInitialRoute:(NSString*)route;
5152

53+
- (id<FlutterPluginRegistry>)pluginRegistry;
54+
5255
@end
5356

5457
#endif // FLUTTER_FLUTTERVIEWCONTROLLER_H_

0 commit comments

Comments
 (0)