url_launcher是用于在移动平台中启动URL的Flutter插件,适用于IOS和Android平台。他可以打开网页,发送邮件,还可以拨打电话。

github地址:https://github.com/flutter/plugins/tree/master/packages/url_launcher

最近项目需求就是打开一个连接跳转到安卓或苹果默认的浏览器。虽然开始一个简单的要求,其中的一个细节就是执行打开网页这一操作后,不能看上去像在应用内部打开,看上去要在应用外部打开。pub.dev 提供了加载网页的插件url_launcher;所谓的插件也是用安卓和苹果原生代码实现的,对插件的代码进行解压可以看到。

加载网页的方式:

_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:https://developer.android.google.cn/guide/webapps/webview?hl=zh_cn

SafariServices:https://developer.apple.com/documentation/safariservices

总结

到此这篇关于Flutter 插件url_launcher简介的文章就介绍到这了,更多相关Flutter 插件url_launcher内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

Flutter 插件url_launcher简介的更多相关文章

  1. Flutter中文教程-Cookbook

    Flutter中文网的Cookbook中包含了在编写Flutter应用程序时常见问题及示例。设计基础使用主题共享颜色和字体样式Images显示来自网上的图片用占位符淡入图片使用缓存图Lists创建一个基本list创建一个水平list使用长列表创建不同类型子项的List创建一个gridList处理手势处理点击添加Material触摸水波效果实现滑动关闭导航导航到新页面并返回给新页面传值从新页面返回数据给上一个页面网络从网上获取数据进行认证请求使用WebSockets

  2. android-studio – 未配置Dart SDK

    Initializinggradle…

  3. 安卓 – 从一个扑动的应用程序拨打电话

    或者有更好的选择从我的应用程序拨打电话?

  4. android – 如何在Flutter中添加Webview?

    我知道可以将WebView添加为整页,但找不到任何示例代码.我假设你可以使用PageView作为它的基础,但不知道如何调用本机androidWebView并将其添加到PageView.谁能指出我正确的方向?

  5. android – 如何将消息从Flutter传递给Native?

    如果需要与特定的API/硬件组件进行交互,您如何将Flutter的信息传递回Android/Native代码?是否有任何事件频道可以通过其他方式发送信息或类似于回调?

  6. android – 如何在Flutter App中处理onPause / onResume?

    我是否过于复杂的事情?即使我的用例似乎不需要它,我仍然想知道:如何自己处理onPause/onResume事件?

  7. android – 如何使用Flutter构建Augment Reality应用程序?

    我对Android开发有一些基础知识.最近听说过Flutter并且非常有兴趣研究它.我想知道是否有可能使用颤振构建增强现实应用程序以及要实现此目的的方法?请帮忙.解决方法截至目前,颤振不支持3D.Flutter现在专注于2D,团队长期计划为颤振提供优化的3Dapi.你读了常见问题here.

  8. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. Flutter StreamBuilder实现局部刷新实例详解

    这篇文章主要为大家介绍了Flutter StreamBuilder实现局部刷新实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. Flutter 首页必用组件NestedScrollView的示例详解

    今天介绍的组件是NestedScrollView,大部分的App首页都会用到这个组件。对Flutter 首页必用组件NestedScrollView的相关知识感兴趣的一起看看吧

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部