AppDelegate.m
/** iOS原生導(dǎo)航欄 */
FlutterViewController *controller = (FlutterViewController *)self.window.rootViewController;
///原生導(dǎo)航欄
ExpressNavigationController *nav = [[ExpressNavigationController alloc ]initWithRootViewController:controller];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
///隱藏原生導(dǎo)航欄,因?yàn)橐褂肍lutter的導(dǎo)航欄布朦。
[controller.navigationController setNavigationBarHidden:YES animated:YES];
/** iOS主動(dòng)通知Flutter -注冊通知 */
[XlbPlugin registerWithRegistrar:[controller registrarForPlugin:@"XlbPlugin"]];
[GeneratedPluginRegistrant registerWithRegistry:controller];
FlutterPlugin.h
#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>
@interface XlbPlugin : NSObject<FlutterPlugin>
/** 發(fā)送信息 */
+ (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配置網(wǎng)上一大堆
//最新微信需要配置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{
///第三方插件也是通過這個(gè)方法處理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"};
///發(fā)消息給Flutter
[XlbPlugin sendMessage:dic];
///APP重新打開的情況下阁将,發(fā)通知給Flutter。Flutter不會(huì)及時(shí)處理右遭,需要在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) {
///使用原生導(dǎo)航欄-跳轉(zhuǎn)原生界面
if ([call.method isEqualToString:@"Webview"]){
ScanWebViewController *webView = [[ScanWebViewController alloc]init];
webView.webUrl = call.arguments;
webView.hidesBottomBarWhenPushed = YES;
///原生導(dǎo)航欄 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(@"現(xiàn)圖地址:%@,圖片大写盗瘛:%.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;
});
}