赞
踩
如果在Plugin中增加下面的代码,增加对AppDelegate中方法的拦截:
[registrar addApplicationDelegate:instance];
需要在应用的AppDelegate中注意
一、如果应用的AppDelegate是继承自FlutterAppDelegate:
@interface AppDelegate : FlutterAppDelegate
@implementation LoginPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"login_plugin"
binaryMessenger:[registrar messenger]];
LoginPlugin* instance = [[LoginPlugin alloc] init];
//注册Appdelegate的回调。
[registrar addApplicationDelegate:instance];
[registrar addMethodCallDelegate:instance channel:channel];
}
则需要注意:
1、如果在AppDelegate中增加了AppDelegate中回调的方法。
如果回调方法没有返回值,不需要加调用supper方法的代码。
如果在AppDelegate中没有实现对应的方法,也是不影响Plugin中注册的回调方法的调用的。
如果此回调方法有返回值,则需要在增加的方法中要调用supper方法,
如下:
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions {
[super application:application willFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions {
[super application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
二、如果AppDelegate没有继承自FlutterAppDelegate,
则在AppDelegate中的每一个回调方法中,都要加入对
_lifeCycleDelegate 的调用。
参考:
https://flutter.cn/docs/development/add-to-app/ios/add-flutter-screen?tab=engine-objective-c-tab
例如:
content_copy
@import Flutter;
@import UIKit;
@import FlutterPluginRegistrant;
@interface AppDelegate : UIResponder <UIApplicationDelegate, FlutterAppLifeCycleProvider>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,strong) FlutterEngine *flutterEngine;
@end
在具体实现中,应该最大化地委托给 FlutterPluginAppLifeCycleDelegate:
content_copy
@interface AppDelegate ()
@property (nonatomic, strong) FlutterPluginAppLifeCycleDelegate* lifeCycleDelegate;
@end
@implementation AppDelegate
- (instancetype)init {
if (self = [super init]) {
_lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
}
return self;
}
- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id>*))launchOptions {
self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
[self.flutterEngine runWithEntrypoint:nil];
[GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
return [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions];
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。