Flutter 插件url_launcher簡介
url_launcher是用于在移動平臺中啟動URL的Flutter插件,適用于IOS和Android平臺。他可以打開網(wǎng)頁,發(fā)送郵件,還可以撥打電話。
github地址:https://github.com/flutter/plugins/tree/master/packages/url_launcher
最近項目需求就是打開一個連接跳轉(zhuǎn)到安卓或蘋果默認(rèn)的瀏覽器。雖然開始一個簡單的要求,其中的一個細(xì)節(jié)就是執(zhí)行打開網(wǎng)頁這一操作后,不能看上去像在應(yīng)用內(nèi)部打開,看上去要在應(yīng)用外部打開。pub.dev 提供了加載網(wǎng)頁的插件url_launcher;所謂的插件也是用安卓和蘋果原生代碼實現(xiàn)的,對插件的代碼進(jìn)行解壓可以看到。
加載網(wǎng)頁的方式:
_launchURL() async { const url = '要加載的網(wǎng)頁地址'; if (await canLaunch(url)) { await launch(url); } }
簡單查看一下插件的源碼:
Future<bool> launch( String urlString, { bool forceSafariVC, bool forceWebView, bool enableJavaScript, bool enableDomStorage, bool universalLinksOnly, Map<String, String> headers, Brightness statusBarBrightness, }) async { assert(urlString != null); ........................................
屬性:forceSafariVC
/// [forceSafariVC] is only used in iOS with iOS version >= 9.0. By default (when unset), the launcher /// opens web URLs in the Safari View Controller, anything else is opened /// using the default handler on the platform. If set to true, it opens the /// URL in the Safari View Controller. If false, the URL is opened in the /// default browser of the phone. Note that to work with universal links on iOS, /// this must be set to false to let the platform's system handle the URL. /// Set this to false if you want to use the cookies/context of the main browser /// of the app (such as SSO flows). This setting will nullify [universalLinksOnly] /// and will always launch a web content in the built-in Safari View Controller regardless /// if the url is a universal link or not.
forceSafariVC 僅被用于IOS版本為0.9和0.9以上的系統(tǒng)。默認(rèn)情況下不設(shè)置,如果設(shè)置加載網(wǎng)頁連接在Safari視圖控制器打開,其他操作系統(tǒng)打開使用默認(rèn)設(shè)置。如果設(shè)置為true,在Safari視圖控制器打開URL。如果設(shè)置為false,在手機默認(rèn)瀏覽器中打開。注意網(wǎng)頁連接在IOS 平臺操作系統(tǒng)上打開必須設(shè)置為false。如果你想去用cookies在app網(wǎng)頁端實現(xiàn)登錄需要設(shè)置為false。
如果加載在內(nèi)置Safari視圖控制器的網(wǎng)頁內(nèi)容是universal link或不是,設(shè)置universalLinksOnly無效。
Universal Link:(點擊連接打開應(yīng)用)參考、參考
屬性:forceWebView
/// [forceWebView] is an Android only setting. If null or false, the URL is /// always launched with the default browser on device. If set to true, the URL /// is launched in a WebView. Unlike iOS, browser context is shared across /// WebViews.
該屬性只在安卓平臺設(shè)置。如果設(shè)置為false或不設(shè)置,網(wǎng)絡(luò)地址被加載在設(shè)備默認(rèn)瀏覽器。如果設(shè)置為true,網(wǎng)絡(luò)地址被加載在自定義WebView。ios系統(tǒng)的瀏覽器可以共享數(shù)據(jù)。
屬性:enableJavaScript
/// [enableJavaScript] is an Android only setting. If true, WebView enable /// javascript.
該屬性只在安卓平臺設(shè)置。如果為true,webview可加載腳步。
屬性:enableDomStorage
/// [enableDomStorage] is an Android only setting. If true, WebView enable /// DOM storage.
該屬性只在安卓平臺設(shè)置。如果為true,webView加載本地網(wǎng)頁緩存。
屬性:universalLinksOnly
/// [universalLinksOnly] is only used in iOS with iOS version >= 10.0. This setting is only validated /// when [forceSafariVC] is set to false. The default value of this setting is false. /// By default (when unset), the launcher will either launch the url in a browser (when the /// url is not a universal link), or launch the respective native app content (when /// the url is a universal link). When set to true, the launcher will only launch /// the content if the url is a universal link and the respective app for the universal /// link is installed on the user's device; otherwise throw a [PlatformException].
該屬性只在IOS平臺使用并且IOS版本為10.0或10.0以上。當(dāng)前該屬性設(shè)置成false生效。默認(rèn)值是false。默認(rèn)情況下,通過手機手機瀏覽器加載網(wǎng)頁(當(dāng)這個鏈接不是一個universal link)或 加載各自app(當(dāng)這個鏈接是一個universal link,點擊進(jìn)行下載應(yīng)用包)。如果設(shè)置屬性值為true,如果這個連接是一個universal link并且各自的應(yīng)用通過這個universal link安裝在用戶的設(shè)備上,那么改網(wǎng)頁會被加載。否則拋出PlatformException。
屬性:statusBarBrightness
/// [statusBarBrightness] Sets the status bar brightness of the application /// after opening a link on iOS. Does nothing if no value is passed. This does /// not handle resetting the previous status bar style.
設(shè)置的狀態(tài)欄亮度在IOS應(yīng)用打開一個連接后可以看到。如果沒有設(shè)置該屬性不會有效果的。狀態(tài)欄樣式重復(fù)設(shè)置以第一次設(shè)置為準(zhǔn)。
Future<bool> launch( String urlString, { bool forceSafariVC, bool forceWebView, bool enableJavaScript, bool enableDomStorage, bool universalLinksOnly, Map<String, String> headers, Brightness statusBarBrightness, }) async { assert(urlString != null); final Uri url = Uri.parse(urlString.trimLeft()); final bool isWebURL = url.scheme == 'http' || url.scheme == 'https'; if ((forceSafariVC == true || forceWebView == true) && !isWebURL) { throw PlatformException( code: 'NOT_A_WEB_SCHEME', message: 'To use webview or safariVC, you need to pass' 'in a web URL. This $urlString is not a web URL.'); } bool previousAutomaticSystemUiAdjustment; if (statusBarBrightness != null && defaultTargetPlatform == TargetPlatform.iOS) { previousAutomaticSystemUiAdjustment = WidgetsBinding.instance.renderView.automaticSystemUiAdjustment; WidgetsBinding.instance.renderView.automaticSystemUiAdjustment = true; SystemChrome.setSystemUIOverlayStyle(statusBarBrightness == Brightness.light ? SystemUiOverlayStyle.dark : SystemUiOverlayStyle.light); } final bool result = await UrlLauncherPlatform.instance.launch( urlString, useSafariVC: forceSafariVC ?? isWebURL, useWebView: forceWebView ?? false, enableJavaScript: enableJavaScript ?? false, enableDomStorage: enableDomStorage ?? false, universalLinksOnly: universalLinksOnly ?? false, headers: headers ?? <String, String>{}, ); if (statusBarBrightness != null) { WidgetsBinding.instance.renderView.automaticSystemUiAdjustment = previousAutomaticSystemUiAdjustment; } return result; }
安卓或蘋果平臺加載:
實現(xiàn)讓用戶看到不少應(yīng)用內(nèi)部跳轉(zhuǎn)打開網(wǎng)頁加載,是跳轉(zhuǎn)到手機默認(rèn)瀏覽器加載。
if (Platform.isIOS) { launch(url, forceSafariVC: false, forceWebView: true); return; } if (Platform.isAndroid) { launch(url); }
解壓插件源碼可以看到Flutter就是調(diào)用安卓或者ios原生代碼進(jìn)行加載網(wǎng)頁。
安卓中通過webview加載網(wǎng)頁或者跳轉(zhuǎn)默認(rèn)瀏覽器加載網(wǎng)頁:
LaunchStatus launch( String url, Bundle headersBundle, boolean useWebView, boolean enableJavaScript, boolean enableDomStorage) { if (activity == null) { return LaunchStatus.NO_ACTIVITY; } Intent launchIntent; if (useWebView) { launchIntent = WebViewActivity.createIntent( activity, url, enableJavaScript, enableDomStorage, headersBundle); } else { launchIntent = new Intent(Intent.ACTION_VIEW) .setData(Uri.parse(url)) .putExtra(Browser.EXTRA_HEADERS, headersBundle); } activity.startActivity(launchIntent); return LaunchStatus.OK; }
在ios手機中默認(rèn)瀏覽器打開
- (void)launchURLInVC:(NSString *)urlString result:(FlutterResult)result API_AVAILABLE(ios(9.0)) { NSURL *url = [NSURL URLWithString:urlString]; self.currentSession = [[FLTURLLaunchSession alloc] initWithUrl:url withFlutterResult:result]; __weak typeof(self) weakSelf = self; self.currentSession.didFinish = ^(void) { weakSelf.currentSession = nil; }; [self.topViewController presentViewController:self.currentSession.safari animated:YES completion:nil]; }
在ios中用內(nèi)置瀏覽器打開:
- (void)launchURL:(NSString *)urlString call:(FlutterMethodCall *)call result:(FlutterResult)result { NSURL *url = [NSURL URLWithString:urlString]; UIApplication *application = [UIApplication sharedApplication]; if (@available(iOS 10.0, *)) { NSNumber *universalLinksOnly = call.arguments[@"universalLinksOnly"] ?: @0; NSDictionary *options = @{UIApplicationOpenURLOptionUniversalLinksOnly : universalLinksOnly}; [application openURL:url options:options completionHandler:^(BOOL success) { result(@(success)); }]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" BOOL success = [application openURL:url]; #pragma clang diagnostic pop result(@(success)); } }
如果在安卓或者蘋果加載http網(wǎng)頁出現(xiàn)無法加載:
///安卓:在xml文件夾下創(chuàng)建network_security_config.xml ,然后在AndroidManifest.xml 標(biāo)簽application引用
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true"> <trust-anchors> <certificates src="system" /> </trust-anchors> </base-config> </network-security-config> ///IOS:
參考:
Android WebView:https://developer.android.google.cn/guide/webapps/webview?hl=zh_cn
SafariServices:https://developer.apple.com/documentation/safariservices
總結(jié)
到此這篇關(guān)于Flutter 插件url_launcher簡介的文章就介紹到這了,更多相關(guān)Flutter 插件url_launcher內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用AndroidStudio上傳忽略文件至SVN Server的解決辦法
這篇文章主要介紹了使用AndroidStudio上傳忽略文件至SVN Server的解決辦法 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06Android自定義View實現(xiàn)簡單文字描邊功能
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)簡單文字描邊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android 進(jìn)度條按鈕ProgressButton的實現(xiàn)代碼
這篇文章主要介紹了Android 進(jìn)度條按鈕實現(xiàn)(ProgressButton)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2018-10-10Android利用Intent實現(xiàn)記事本功能(NotePad)
這篇文章主要為大家詳細(xì)介紹了Android利用Intent實現(xiàn)簡單記事本功能(NotePad)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06