美文网首页Flutter
Flutter (半年开发经验)- iOS原生导航栏、iOS主动

Flutter (半年开发经验)- iOS原生导航栏、iOS主动

作者: 吃货_X | 来源:发表于2019-12-05 16:30 被阅读0次

    AppDelegate.m

    /** iOS原生导航栏 */
    FlutterViewController *controller = (FlutterViewController *)self.window.rootViewController;
    ///原生导航栏
    ExpressNavigationController *nav = [[ExpressNavigationController alloc ]initWithRootViewController:controller];
     self.window.rootViewController = nav;
     [self.window makeKeyAndVisible];
    ///隐藏原生导航栏,因为要使用Flutter的导航栏。
     [controller.navigationController setNavigationBarHidden:YES animated:YES];
    
    /** iOS主动通知Flutter -注册通知 */
    [XlbPlugin registerWithRegistrar:[controller registrarForPlugin:@"XlbPlugin"]];
     [GeneratedPluginRegistrant registerWithRegistry:controller];
    

    FlutterPlugin.h

    #import <Foundation/Foundation.h>
    #import <Flutter/Flutter.h>
    
    @interface XlbPlugin : NSObject<FlutterPlugin>
    
    /** 发送信息 */
    + (void)sendMessage:(NSDictionary *)dic;
    
    /** 处理OpenURL */
    + (void)handleOpenURL:(NSURL*)url;
    
    + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar;
    
    @end
    

    FlutterPlugin.m

    #import "XlbPlugin.h"
    
    
    __weak XlbPlugin* __XlbPlugin;
    
    @interface XlbPlugin()
    
    @property (copy,nonatomic) FlutterBasicMessageChannel *channel;
    
    @end
    @implementation XlbPlugin
    
    
    -(id)init{
        if(self = [super init]){
            __XlbPlugin  = self;
        }
        return self;
    }
    
    + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar{
        FlutterBasicMessageChannel *basicMessage_channel = [FlutterBasicMessageChannel messageChannelWithName:@"XlbBasicMessage_Plugin" binaryMessenger:[registrar messenger]];
        FlutterMethodChannel *method_Channel = [FlutterMethodChannel
                                         methodChannelWithName:@"XlbMethod_Plugin"
                                         binaryMessenger:[registrar messenger]];
        XlbPlugin *instance = [[XlbPlugin alloc] init];
        [registrar addMethodCallDelegate:instance channel:method_Channel];
        [__XlbPlugin channelSet:basicMessage_channel];
    }
    //MARK: - Getter/Setter
    - (void)channelSet:(FlutterBasicMessageChannel *)channel{
        _channel = channel;
    }
    
    + (void)sendMessage:(NSDictionary *)dic{
        [__XlbPlugin sendMessage:dic];
    }
    
    + (void)handleOpenURL:(NSURL*)url{
        [__XlbPlugin openUrl:url];
    }
    
    - (void)sendMessage:(NSDictionary *)dic{
        [self.channel sendMessage:dic];
    }
    - (void)openUrl:(NSURL *)url{
        [self.channel sendMessage:[NSString stringWithFormat:@"%@",url]];
    }
    

    最新微信SDK处理-universalLink配置网上一大堆

    //最新微信需要配置universalLink,配置后微信打开APP走的下面方法。未配置走的openapplication:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
    
    - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
        ///第三方插件也是通过这个方法处理URL
       [super application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
        NSURL *url = userActivity.webpageURL;
        NSString *path = url.path;
      //这儿处理的是分享链接的。
        if ([path rangeOfString:appid].location != NSNotFound) {
            [WXApi handleOpenURL:url delegate:(id<WXApiDelegate>)self];
        }
        if ([path rangeOfString:@"share"].location != NSNotFound) {
            NSDictionary *dic = @{@"share":[self diwWithUrl:userActivity.webpageURL.absoluteString],@"key":@"share"};
            ///发消息给Flutter
            [XlbPlugin sendMessage:dic];
            ///APP重新打开的情况下,发通知给Flutter。Flutter不会及时处理,需要在APP打开后通过粘贴板处理链接。
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = url.absoluteString;
        }
        return YES;
    }
    

    处理Flutter通知原生-图片压缩(需要image_Picker)

    FlutterMethodChannel *shareChannel = [FlutterMethodChannel methodChannelWithName:@"NativeCode" binaryMessenger:controller];
        [shareChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
          
          ///使用原生导航栏-跳转原生界面
          if ([call.method isEqualToString:@"Webview"]){
                ScanWebViewController *webView = [[ScanWebViewController alloc]init];
                webView.webUrl = call.arguments;
                webView.hidesBottomBarWhenPushed = YES;
                ///原生导航栏 ExpressNavigationController *nav = [[ExpressNavigationController alloc ]initWithRootViewController:controller];
                [nav pushViewController:webView animated:YES];
            }
            if ([call.method isEqualToString:@"imageEdit"]){
                //原图地址
                NSString *originalImageFile = call.arguments;
                NSData *originalImageData = [NSData dataWithContentsOfFile:originalImageFile];
                UIImage *originalImage = [UIImage imageWithData:originalImageData];
                CGFloat soriginalKB = originalImageData.length / 1024.0;
                NSLog(@"原图地址:%@,图片大小:%.2fKB",originalImageFile,soriginalKB);
                if (soriginalKB < 500){
                    result(originalImageFile);
                } else{
                    //处理后图片
                    NSData *imageDate = [NetworkingRequest reSizeImageData:originalImage maxImageSize:0 maxSizeWithKB:500];
                    UIImage *editImage = [UIImage imageWithData:imageDate];
                    ///用imagePicker保存图片,返回路径
                    NSString *imagePath = [FLTImagePickerPhotoAssetUtil saveImageWithOriginalImageData:originalImageData image:editImage maxWidth:[NSNumber numberWithFloat:originalImage.size.width] maxHeight:[NSNumber numberWithFloat:originalImage.size.height] imageQuality:[NSNumber numberWithInt:1]];
                    //处理后图片大小
                    CGFloat sizeOriginKB = imageDate.length / 1024.0;
                    NSLog(@"现图地址:%@,图片大小:%.2fKB",imagePath,sizeOriginKB);
                    result(imagePath);
                }
            }
        }];
    

    Flutter端处理

    static const BasicMessageChannel<dynamic> iosMessage =
    const BasicMessageChannel(
    "XlbBasicMessageNSDictionary_Plugin", StandardMessageCodec());
    
    @override
    void initState() {
    super.initState();
    iosMessage.setMessageHandler((dynamic value) {
    print('收到iOS原生消息:' + value.toString());
    //我原生传过来的是NSDictionary,所以Flutter用Map接收
    Map dic = value;
    });
    }
    

    相关文章

      网友评论

        本文标题:Flutter (半年开发经验)- iOS原生导航栏、iOS主动

        本文链接:https://www.haomeiwen.com/subject/flodgctx.html