launchUrl function
- Uri url,
- {LaunchMode mode = LaunchMode.platformDefault,
- WebViewConfiguration webViewConfiguration = const WebViewConfiguration(),
- String? webOnlyWindowName}
Passes url
to the underlying platform for handling.
mode
support varies significantly by platform:
- LaunchMode.platformDefault is supported on all platforms:
- On iOS and Android, this treats web URLs as LaunchMode.inAppWebView, and all other URLs as LaunchMode.externalApplication.
- On Windows, macOS, and Linux this behaves like LaunchMode.externalApplication.
- On web, this uses
webOnlyWindowName
for web URLs, and behaves like LaunchMode.externalApplication for any other content.
- LaunchMode.inAppWebView is currently only supported on iOS and Android. If a non-web URL is passed with this mode, an ArgumentError will be thrown.
- LaunchMode.externalApplication is supported on all platforms. On iOS, this should be used in cases where sharing the cookies of the user's browser is important, such as SSO flows, since Safari View Controller does not share the browser's context.
- LaunchMode.externalNonBrowserApplication is supported on iOS 10+. This setting is used to require universal links to open in a non-browser application.
For web, webOnlyWindowName
specifies a target for the launch. This
supports the standard special link target names. For example:
- "_blank" opens the new URL in a new tab.
- "_self" opens the new URL in the current tab. Default behaviour when unset is to open the url in a new tab.
Returns true if the URL was launched successful, otherwise either returns
false or throws a PlatformException
depending on the failure.
Implementation
Future<bool> launchUrl(
Uri url, {
LaunchMode mode = LaunchMode.platformDefault,
WebViewConfiguration webViewConfiguration = const WebViewConfiguration(),
String? webOnlyWindowName,
}) async {
if (mode == LaunchMode.inAppWebView &&
!(url.scheme == 'https' || url.scheme == 'http')) {
throw ArgumentError.value(url, 'url',
'To use an in-app web view, you must provide an http(s) URL.');
}
return UrlLauncherPlatform.instance.launchUrl(
url.toString(),
LaunchOptions(
mode: convertLaunchMode(mode),
webViewConfiguration: convertConfiguration(webViewConfiguration),
webOnlyWindowName: webOnlyWindowName,
),
);
}