1.錯誤提示
2019-08-01 18:57:29.445804+0800 pedi Dev[48916:362606] [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: PlatformException(unregistered_view_type, trying to create a view with an unregistered type, unregistered view type: 'plugins.flutter.io/webview')
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
#1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
#2 PlatformViewsService.initUiKitView (package:flutter/src/services/platform_views.dart:167:41)
#3 _UiKitViewState._createNewUiKitView (package:flutter/src/widgets/platform_view.dart:499:71)
#4 _UiKitViewState._initializeOnce (package:flutter/src/widgets/platform_view.dart:449:5)
#5 _UiKitViewState.didChangeDependencies (package:flutter/src/widgets/platform_view.dart:459:5)
webview加載失敗說明兩個問題:
1.plugin注冊失敗
2.ios工程在info.plist 需要加
<key>io.flutter.embedded_views_preview</key>
如果url是http胸竞,還需要加
<key>NSAppTransportSecurity</key>
? ? ? ? ? <key>NSAllowsArbitraryLoads</key>
? ? ? ? <true/>
? ? ? ? <key>NSAllowsArbitraryLoadsInWebContent</key>
? ? ? ? <true/>
iOS Platform
FlutterAppDelegate煞聪,是iOS的Plugin管理器萍诱,它記錄了所有的Plugin蹄胰,并將Plugin綁定到FlutterViewController(默認是rootViewController)丈攒。官方Demo是個StoryBoard荧降,以及純flutter項目他們的RootViewController默認就是FlutterViewController冠骄。而Plugin注冊很重要的一點是撒妈,只有注冊到FlutterViewController廓旬,才能注冊成功,才可以加載執(zhí)行Flutter第三方插件抱既。否則就會出現(xiàn)上述問題听怕。而混合開發(fā)的工程絕大多數(shù)的RootViewController都不可能是FlutterViewController捧挺,一定是個Native的頁面比如TabbarController或者NavigationController,所以注冊一定失敗尿瞭。所以闽烙,如果出現(xiàn)這種情況,下面是閑魚筷厘,以及官方的注冊Plugin的代碼教程鸣峭。
FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithEngine:self.engine nibName:nil bundle:nil];?
self.flutterEngine= [[FlutterEnginealloc]initWithName:@"io.flutter"project:nil];? [self.flutterEnginerunWithEntrypoint:nil];? [GeneratedPluginRegistrantregisterWithRegistry:self.flutterEngine];
經(jīng)過多次運行測試,發(fā)現(xiàn)這三行代碼竟然會有時效性的問題酥艳。比如老工程的didFinishLaunchingWithOptions方法里面 一定有很多的第三方模塊的初始化代碼 以及自己工程的網(wǎng)絡工具初始化等各個模塊的代碼摊溶,所以中間的耗時操作,會影響到上面的Plugin注冊充石。經(jīng)常創(chuàng)建失敗莫换。 用flutterengine 去注冊 給appdelegate 加一個flutterengine的屬性。然后創(chuàng)建fluttervc也要用initwithengine 的方式骤铃。你以為直接運行就完事了拉岁?然并卵。 運行白屏惰爬。使用延遲加載也不管用喊暖,網(wǎng)上搜索到的教程
經(jīng)過大神指點要先使用present然后dismiss才能顯示出來
__weak __typeof(self)weakSelf = self;? ? ? ? self.ctr4.modalPresentationStyle = UIModalPresentationOverCurrentContext;? ? ? ? [self presentViewController:weakSelf.ctr4 animated:NO completion:^{? ? ? ? ? ? [weakSelf dismissViewControllerAnimated:NO completion:^{? ? ? ? ? ? ? ? [weakSelf addChildViewController:weakSelf.ctr4];? ? ? ? ? ? ? ? [weakSelf.view bringSubviewToFront:weakSelf.tabbarContainer];? ? ? ? ? ? }];? ? ? ? }];
接下來準備添加多個flutter
然而在push過程中發(fā)現(xiàn)flutter的第一次顯示的界面竟然是上次tab的頁面,因為engine是同一份的撕瞧,我們創(chuàng)建的時候會保存一份engine陵叽。
路由傳值我們之前會用到- (void)setInitialRoutez:(NSString*)route ,但是用Engine注冊你會發(fā)現(xiàn)傳入到main.dart里面的window.defaultRouteName一直是 / 根目錄符號丛版,這時候巩掺,Native初始化Flutter頁面的代碼以及傳后續(xù)請求頭的路無情中被堵的死死的。所以你只能用消息發(fā)送的機制來解決多路由傳值的功能页畦,要用到FlutterMethodChannel胖替,EventChannel,BasicMessageChannel,雖然很常用独令,但是對于初始化路由來講端朵,過于繁雜,復雜度高记焊。
最后的最后逸月。經(jīng)過了無數(shù)次,復雜遍膜,漫長,絕望瓤湘,心灰意冷的嘗試后瓢颅。
發(fā)現(xiàn)? 你只需要把這行代碼?
[GeneratedPluginRegistrant registerWithRegistry:self];
放到你需要被插件響應的FlutterViewController頁面里面,就解決了上面所有的問題弛说。
做了最近一段時間flutter的理解就是挽懦。flutter他整個底層還是不夠完善不夠充足。就相當于木人。你從點A到B信柿。他只有一條或者兩條路,
你只能走著走著醒第,前面有個坑渔嚷,你跳進去,然后再爬上了稠曼,再往前走形病,再跳進去再爬上來。就像實現(xiàn)一個功能霞幅。他只能那么一個或者兩個方法才可以實現(xiàn)漠吻,你沒法繞過或者通過別的實現(xiàn)方式去做,就是你只能這樣搞而不是說像現(xiàn)在比較成熟的一些比如說IOS司恳,安卓或者是H5途乃。他整個從A到B從這走到那兒。走不過去扔傅,你可以換條別的路過去耍共。就這些不夠豐富的api是flutter底層比較欠缺的,不夠完備的地方铅鲤,
這也是任何一門語言的必經(jīng)之路划提,隨著開發(fā)者的不斷加入,牽線搭橋邢享,疊加最偉大公司Google 的背書鹏往。整個flutter還是會馬不停蹄的發(fā)展壯大的。
文章參考:
http://www.reibang.com/p/5e5d54db8c7e? Flutter和原生iOS交互(GA_)
http://www.reibang.com/p/119fc6873434? Flutter Plugin 調用 Native APIs(閑魚技術)
https://juejin.im/post/5c6e84156fb9a049a5718047? flutter 多實例實戰(zhàn)(共田君)
https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps? Google自家混合集成Wiki