当前位置:   article > 正文

Flutter的plugin中对AppDelegate中的方法进行拦截_ios registrar addapplicationdelegate:instance

ios registrar addapplicationdelegate:instance


如果在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];
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/145262
推荐阅读
相关标签
  

闽ICP备14008679号