Flutter 插件url_launcher简介
url_launcher是用于在移动平台中启动url的flutter插件,适用于ios和android平台。他可以打开网页,发送邮件,还可以拨打电话。
github地址:
最近项目需求就是打开一个连接跳转到安卓或苹果默认的浏览器。虽然开始一个简单的要求,其中的一个细节就是执行打开网页这一操作后,不能看上去像在应用内部打开,看上去要在应用外部打开。提供了加载网页的插件;所谓的插件也是用安卓和苹果原生代码实现的,对插件的代码进行解压可以看到。
加载网页的方式:
_launchurl() async { const url = '要加载的网页地址'; 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以上的系统。默认情况下不设置,如果设置加载网页连接在safari视图控制器打开,其他操作系统打开使用默认设置。如果设置为true,在safari视图控制器打开url。如果设置为false,在手机默认浏览器中打开。注意网页连接在ios 平台操作系统上打开必须设置为false。如果你想去用cookies在app网页端实现登录需要设置为false。
如果加载在内置safari视图控制器的网页内容是universal link或不是,设置universallinksonly无效。
universal link:(点击连接打开应用)参考、参考
属性: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.
该属性只在安卓平台设置。如果设置为false或不设置,网络地址被加载在设备默认浏览器。如果设置为true,网络地址被加载在自定义webview。ios系统的浏览器可以共享数据。
属性:enablejavascript
/// [enablejavascript] is an android only setting. if true, webview enable /// javascript.
该属性只在安卓平台设置。如果为true,webview可加载脚步。
属性:enabledomstorage
/// [enabledomstorage] is an android only setting. if true, webview enable /// dom storage.
该属性只在安卓平台设置。如果为true,webview加载本地网页缓存。
属性: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以上。当前该属性设置成false生效。默认值是false。默认情况下,通过手机手机浏览器加载网页(当这个链接不是一个universal link)或 加载各自app(当这个链接是一个universal link,点击进行下载应用包)。如果设置属性值为true,如果这个连接是一个universal link并且各自的应用通过这个universal link安装在用户的设备上,那么改网页会被加载。否则抛出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.
设置的状态栏亮度在ios应用打开一个连接后可以看到。如果没有设置该属性不会有效果的。状态栏样式重复设置以第一次设置为准。
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; }
安卓或苹果平台加载:
实现让用户看到不少应用内部跳转打开网页加载,是跳转到手机默认浏览器加载。
if (platform.isios) { launch(url, forcesafarivc: false, forcewebview: true); return; } if (platform.isandroid) { launch(url); }
解压插件源码可以看到flutter就是调用安卓或者ios原生代码进行加载网页。
安卓中通过webview加载网页或者跳转默认浏览器加载网页:
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手机中默认浏览器打开
- (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中用内置浏览器打开:
- (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网页出现无法加载:
///安卓:在xml文件夹下创建network_security_config.xml ,然后在androidmanifest.xml 标签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:
safariservices:
总结
到此这篇关于flutter 插件url_launcher简介的文章就介绍到这了,更多相关flutter 插件url_launcher内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!