### Example pubspec.yaml Entry Source: https://pub.dev/packages/url_launcher_macos/install This is an example of how the url_launcher_macos dependency will appear in your pubspec.yaml file after running the add command. ```yaml dependencies: url_launcher_macos: ^3.2.5 ``` -------------------------------- ### Launch File URL on Desktop Source: https://pub.dev/packages/url_launcher This example demonstrates launching a file URL on desktop platforms. It includes a check to ensure the file exists before attempting to launch it. ```dart final String filePath = testFile.absolute.path; final Uri uri = Uri.file(filePath); if (!File(uri.toFilePath()).existsSync()) { throw Exception('$uri does not exist!'); } if (!await launchUrl(uri)) { throw Exception('Could not launch $uri'); } ``` -------------------------------- ### Add url_launcher_windows to pubspec.yaml Source: https://pub.dev/packages/url_launcher_windows/install This is an example of how the url_launcher_windows dependency will appear in your pubspec.yaml file after running the add command. ```yaml dependencies: url_launcher_windows: ^3.1.5 ``` -------------------------------- ### Update pubspec.yaml Source: https://pub.dev/packages/url_launcher_ios/install This is an example of how the `url_launcher_ios` dependency will appear in your `pubspec.yaml` file after running the add command. ```yaml dependencies: url_launcher_ios: ^6.4.1 ``` -------------------------------- ### Add url_launcher_android to pubspec.yaml Source: https://pub.dev/packages/url_launcher_android/install This is an example of how the url_launcher_android dependency should appear in your pubspec.yaml file. ```yaml dependencies: url_launcher_android: ^6.3.32 ``` -------------------------------- ### Import url_launcher_windows in Dart Source: https://pub.dev/packages/url_launcher_windows/install Import the package into your Dart code to start using its functionalities. ```dart import 'package:url_launcher_windows/url_launcher_windows.dart'; ``` -------------------------------- ### Launch a URL in Flutter Source: https://pub.dev/packages/url_launcher This example demonstrates how to launch a URL using the url_launcher package. Ensure the `launchUrl` function is called within a user-triggered event, such as a button press, to comply with browser restrictions. ```dart import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; final Uri _url = Uri.parse('https://flutter.dev'); void main() => runApp( const MaterialApp( home: Material( child: Center( child: ElevatedButton( onPressed: _launchUrl, child: Text('Show Flutter homepage'), ), ), ), ), ); Future _launchUrl() async { if (!await launchUrl(_url)) { throw Exception('Could not launch $_url'); } } ``` -------------------------------- ### Launch URL in Browser (Dart) Source: https://pub.dev/packages/url_launcher_linux/example Launches a given URL in the default browser. Ensure the URL can be launched using `canLaunch` before attempting to launch. This example uses `UrlLauncherPlatform.instance.launch` for browser-based launching. ```dart // Copyright 2013 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'package:flutter/material.dart'; import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'URL Launcher', theme: ThemeData(primarySwatch: Colors.blue), home: const MyHomePage(title: 'URL Launcher'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { Future? _launched; Future _launchInBrowser(String url) async { if (await UrlLauncherPlatform.instance.canLaunch(url)) { await UrlLauncherPlatform.instance.launch( url, useSafariVC: false, useWebView: false, enableJavaScript: false, enableDomStorage: false, universalLinksOnly: false, headers: {}, ); } else { throw Exception('Could not launch $url'); } } Widget _launchStatus(BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { return const Text(''); } } @override Widget build(BuildContext context) { const String toLaunch = 'https://www.cylog.org/headers/'; return Scaffold( appBar: AppBar(title: Text(widget.title)), body: ListView( children: [ Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Padding( padding: EdgeInsets.all(16.0), child: Text(toLaunch), ), ElevatedButton( onPressed: () => setState(() { _launched = _launchInBrowser(toLaunch); }), child: const Text('Launch in browser'), ), const Padding(padding: EdgeInsets.all(16.0)), FutureBuilder(future: _launched, builder: _launchStatus), ], ), ], ), ); } } ``` -------------------------------- ### Add url_launcher_ios Dependency Source: https://pub.dev/packages/url_launcher_ios/install Run this command in your terminal to add the url_launcher_ios package to your project's dependencies. This command also implicitly runs `flutter pub get`. ```bash flutter pub add url_launcher_ios ``` -------------------------------- ### Add url_launcher_linux to pubspec.yaml Source: https://pub.dev/packages/url_launcher_linux/install This is an example of how the url_launcher_linux dependency will appear in your pubspec.yaml file after running `flutter pub add`. ```yaml dependencies: url_launcher_linux: ^3.2.2 ``` -------------------------------- ### Declare url_launcher_web Dependency in pubspec.yaml Source: https://pub.dev/packages/url_launcher_web/install This is an example of how the url_launcher_web dependency will appear in your pubspec.yaml file after running `flutter pub add`. Ensure the version matches the latest compatible version. ```yaml dependencies: url_launcher_web: ^2.4.3 ``` -------------------------------- ### Launch Universal Link on iOS Source: https://pub.dev/packages/url_launcher/example Attempts to launch a universal link on iOS, which can open a native app if installed, otherwise falls back to opening the URL in Safari. ```Dart final Uri toLaunch = Uri(scheme: 'https', host: 'www.cylog.org', path: 'headers/'); // ... inside a button's onPressed callback _launchUniversalLinkIOS(toLaunch) ``` -------------------------------- ### Android URL Scheme Configuration Source: https://pub.dev/packages/url_launcher On Android, starting from API level 30, you need to declare URL schemes in your `AndroidManifest.xml` within a `` element. This is required for `canLaunchUrl` to return true for most schemes and for `supportsLaunchMode(LaunchMode.inAppBrowserView)`. ```xml ``` -------------------------------- ### Basic Test App for url_launcher_web Source: https://pub.dev/packages/url_launcher_web/example This is the main entry point for a basic Flutter application used for testing the url_launcher_web package. It displays a simple text and logs results to the console. ```dart // Copyright 2013 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } /// App for testing class MyApp extends StatefulWidget { /// Default Constructor const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { @override Widget build(BuildContext context) { return const Directionality( textDirection: TextDirection.ltr, child: Text('Testing... Look at the console output for results!'), ); } } ``` -------------------------------- ### Launch URL in Browser Source: https://pub.dev/packages/url_launcher_ios/example Launches a URL in the default browser. Ensure the URL is valid. ```dart ElevatedButton( onPressed: () => setState(() { _launched = _launchInBrowser(toLaunch); }), child: const Text('Launch in browser'), ), ``` -------------------------------- ### Link Widget Documentation Source: https://pub.dev/packages/url_launcher/example Provides a button that navigates to the documentation for the Link Widget. This is useful for users who want to learn more about creating links. ```Dart Link( uri: Uri.parse( 'https://pub.dev/documentation/url_launcher/latest/link/link-library.html'), target: LinkTarget.blank, builder: (BuildContext ctx, FollowLink? openLink) { return TextButton.icon( onPressed: openLink, label: const Text('Link Widget documentation'), icon: const Icon(Icons.read_more), ); }, ) ``` -------------------------------- ### Launch In-App Browser with Options Source: https://pub.dev/packages/url_launcher/example Launches a URL in an in-app browser view with custom options, such as showing the title bar. ```dart Future _launchInAppWithBrowserOptions(Uri url) async { if (!await launchUrl( url, mode: LaunchMode.inAppBrowserView, browserConfiguration: const BrowserConfiguration(showTitle: true), )) { throw Exception('Could not launch $url'); } } ``` -------------------------------- ### Launch Universal Link iOS Source: https://pub.dev/packages/url_launcher_ios/example Launches a universal link on iOS, falling back to Safari if the native app is not installed or configured to handle the link. This is ideal for deep linking into native applications. ```dart ElevatedButton( onPressed: () => setState(() { _launched = _launchUniversalLinkIos(toLaunch); }), child: const Text( 'Launch a universal link in a native app, fallback to Safari.(Youtube)', ), ), ``` -------------------------------- ### Launch URL in App Source: https://pub.dev/packages/url_launcher_ios/example Launches a URL within the app's web view. This is useful for displaying web content inline. ```dart ElevatedButton( onPressed: () => setState(() { _launched = _launchInWebViewOrVC(toLaunch); }), child: const Text('Launch in app'), ), ``` -------------------------------- ### Initialize URL Launcher and Check Call Support Source: https://pub.dev/packages/url_launcher_ios/example Initializes the URL launcher and checks if the device supports making phone calls. This is typically done in the initState method of a StatefulWidget. ```dart // Copyright 2013 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'package:flutter/material.dart'; import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'URL Launcher', theme: ThemeData(primarySwatch: Colors.blue), home: const MyHomePage(title: 'URL Launcher'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { bool _hasCallSupport = false; Future? _launched; String _phone = ''; @override void initState() { super.initState(); // Check for phone call support. final UrlLauncherPlatform launcher = UrlLauncherPlatform.instance; launcher.canLaunch('tel://123').then((bool result) { if (!mounted) { return; } setState(() { _hasCallSupport = result; }); }); } Future _launchInBrowser(String url) async { final UrlLauncherPlatform launcher = UrlLauncherPlatform.instance; if (!await launcher.launchUrl( url, const LaunchOptions(mode: PreferredLaunchMode.externalApplication), )) { throw Exception('Could not launch $url'); } } Future _launchInWebViewOrVC(String url) async { final UrlLauncherPlatform launcher = UrlLauncherPlatform.instance; if (!await launcher.launchUrl( url, const LaunchOptions(mode: PreferredLaunchMode.inAppBrowserView), )) { throw Exception('Could not launch $url'); } } Future _launchUniversalLinkIos(String url) async { final UrlLauncherPlatform launcher = UrlLauncherPlatform.instance; final bool nativeAppLaunchSucceeded = await launcher.launchUrl( url, const LaunchOptions( mode: PreferredLaunchMode.externalNonBrowserApplication, ), ); if (!nativeAppLaunchSucceeded) { await launcher.launchUrl( url, const LaunchOptions(mode: PreferredLaunchMode.inAppBrowserView), ); } } Widget _launchStatus(BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { return const Text(''); } } Future _makePhoneCall(String url) async { final UrlLauncherPlatform launcher = UrlLauncherPlatform.instance; if (!await launcher.launchUrl(url, const LaunchOptions())) { throw Exception('Could not launch $url'); } } ``` -------------------------------- ### Launch URL in Browser Source: https://pub.dev/packages/url_launcher_android/example Launches a URL in the default external browser. This method assumes that every device can launch a web URL and does not require a 'canLaunch' check. ```dart ElevatedButton( onPressed: () => setState(() { _launched = _launchInBrowser(toLaunch); }), child: const Text('Launch in browser'), ) ``` -------------------------------- ### MIT License Source: https://pub.dev/packages/url_launcher/license This section displays the license text for the url_launcher package. It outlines the terms under which the software can be used, modified, and distributed. ```text Copyright 2013 The Flutter Authors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Google Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``` -------------------------------- ### Add url_launcher_windows Dependency Source: https://pub.dev/packages/url_launcher_windows/install Run this command in your terminal to add the package to your Flutter project. This automatically updates your pubspec.yaml file. ```bash flutter pub add url_launcher_windows ``` -------------------------------- ### Launch URL in External Browser Source: https://pub.dev/packages/url_launcher_android/example Launches a URL in the default external browser. Ensure the URL is valid and accessible. ```dart Future _launchInBrowser(String url) async { if (!await launcher.launchUrl( url, const LaunchOptions(mode: PreferredLaunchMode.externalApplication), )) { throw Exception('Could not launch $url'); } } ``` -------------------------------- ### Import url_launcher_platform_interface in Dart Source: https://pub.dev/packages/url_launcher_platform_interface/install Import the package into your Dart files to use its functionalities. ```dart import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; ``` -------------------------------- ### Add url_launcher Dependency Source: https://pub.dev/packages/url_launcher/install Run this command to add the url_launcher package to your Flutter project's dependencies. ```bash $ flutter pub add url_launcher ``` -------------------------------- ### Launch URL in Web View Source: https://pub.dev/packages/url_launcher_android/example Launches a URL within a web view. This provides an in-app browsing experience. ```dart ElevatedButton( onPressed: () => setState(() { _launched = _launchInWebView(toLaunch); }), child: const Text('Launch in web view'), ) ``` -------------------------------- ### Launch URL in App (InAppWebView) Source: https://pub.dev/packages/url_launcher/example Launches a URL within the application using an in-app web view. This provides a more integrated browsing experience. ```Dart final Uri toLaunch = Uri(scheme: 'https', host: 'www.cylog.org', path: 'headers/'); // ... inside a button's onPressed callback _launchInBrowserView(toLaunch) ``` -------------------------------- ### Add url_launcher_linux Dependency Source: https://pub.dev/packages/url_launcher_linux/install Run this command to add the url_launcher_linux package to your Flutter project's dependencies. ```bash $ flutter pub add url_launcher_linux ``` -------------------------------- ### Add url_launcher_web Dependency Source: https://pub.dev/packages/url_launcher_web/install Run this command in your Flutter project to add the url_launcher_web package as a dependency. This command automatically updates your pubspec.yaml file. ```bash $ flutter pub add url_launcher_web ``` -------------------------------- ### Launch Universal Link on iOS Source: https://pub.dev/packages/url_launcher/example Attempts to launch a URL as a universal link on iOS. If it fails, it falls back to opening in an in-app browser view. ```dart Future _launchUniversalLinkIOS(Uri url) async { final bool nativeAppLaunchSucceeded = await launchUrl( url, mode: LaunchMode.externalNonBrowserApplication, ); if (!nativeAppLaunchSucceeded) { await launchUrl( url, mode: LaunchMode.inAppBrowserView, ); } } ``` -------------------------------- ### Add url_launcher_platform_interface Dependency Source: https://pub.dev/packages/url_launcher_platform_interface/install Use this command to add the package to your Flutter project. This automatically updates your pubspec.yaml file. ```bash $ flutter pub add url_launcher_platform_interface ``` -------------------------------- ### iOS URL Scheme Configuration Source: https://pub.dev/packages/url_launcher For iOS, you must declare URL schemes that your app checks with `canLaunchUrl` in your Info.plist file. This ensures that the system can correctly identify and handle these schemes. ```xml LSApplicationQueriesSchemes sms tel ``` -------------------------------- ### Launch URL in External Application Source: https://pub.dev/packages/url_launcher/example Launches a URL in an external application, such as a web browser. Use this for general web links. ```dart Future _launchInBrowser(Uri url) async { if (!await launchUrl( url, mode: LaunchMode.externalApplication, )) { throw Exception('Could not launch $url'); } } ``` -------------------------------- ### Import url_launcher_macos in Dart Source: https://pub.dev/packages/url_launcher_macos/install Use this import statement in your Dart code to access the functionality provided by the url_launcher_macos plugin. ```dart import 'package:url_launcher_macos/url_launcher_macos.dart'; ``` -------------------------------- ### Launch URL in Web View and Close After Delay Source: https://pub.dev/packages/url_launcher_android/example Launches a URL in a web view and then automatically closes the web view after a specified delay. This demonstrates programmatic control over the web view lifecycle. ```dart ElevatedButton( onPressed: () => setState(() { _launched = _launchInWebView(toLaunch); Timer(const Duration(seconds: 5), () { launcher.closeWebView(); }); }), child: const Text('Launch in web view + close after 5 seconds'), ) ``` -------------------------------- ### Add url_launcher_android to Flutter Project Source: https://pub.dev/packages/url_launcher_android/install Run this command in your terminal to add the package as a dependency. This will automatically update your pubspec.yaml file. ```bash $ flutter pub add url_launcher_android ``` -------------------------------- ### Add url_launcher_macos Dependency Source: https://pub.dev/packages/url_launcher_macos/install Run this command to add the url_launcher_macos package to your Flutter project's dependencies. ```bash $ flutter pub add url_launcher_macos ``` -------------------------------- ### Launch URL in In-App Browser View Source: https://pub.dev/packages/url_launcher/example Launches a URL within an in-app browser view. This provides a more integrated experience than an external browser. ```dart Future _launchInBrowserView(Uri url) async { if (!await launchUrl(url, mode: LaunchMode.inAppBrowserView)) { throw Exception('Could not launch $url'); } } ``` -------------------------------- ### Import url_launcher in Dart Code Source: https://pub.dev/packages/url_launcher/install Import the url_launcher package into your Dart files to use its functionality. ```dart import 'package:url_launcher/url_launcher.dart'; ``` -------------------------------- ### Import url_launcher_linux in Dart Source: https://pub.dev/packages/url_launcher_linux/install Import the url_launcher_linux package into your Dart code to use its functionalities. ```dart import 'package:url_launcher_linux/url_launcher_linux.dart'; ``` -------------------------------- ### Launch URL in In-App WebView Source: https://pub.dev/packages/url_launcher_android/example Launches a URL within an in-app WebView. This allows for full control over the web content displayed within the app. ```dart Future _launchInWebView(String url) async { if (!await launcher.launchUrl( url, const LaunchOptions(mode: PreferredLaunchMode.inAppWebView), )) { throw Exception('Could not launch $url'); } } ``` -------------------------------- ### Launch URL in Web View (DOM Storage OFF) Source: https://pub.dev/packages/url_launcher_android/example Launches a URL in a web view with DOM storage disabled. This prevents the web view from storing data locally. ```dart ElevatedButton( onPressed: () => setState(() { _launched = _launchInWebViewWithoutDomStorage(toLaunch); }), child: const Text('Launch in web view (DOM storage OFF)'), ) ``` -------------------------------- ### Launch URL in In-App WebView with Custom Headers Source: https://pub.dev/packages/url_launcher_android/example Launches a URL in an in-app WebView and includes custom HTTP headers in the request. This is useful for authentication or passing specific information. ```dart Future _launchInWebViewWithCustomHeaders(String url) async { if (!await launcher.launchUrl( url, const LaunchOptions( mode: PreferredLaunchMode.inAppWebView, webViewConfiguration: InAppWebViewConfiguration( headers: {'my_header_key': 'my_header_value'}, ), ), )) { throw Exception('Could not launch $url'); } } ``` -------------------------------- ### Launch URL in App and Close WebView Source: https://pub.dev/packages/url_launcher_ios/example Launches a URL in the app's web view and automatically closes the web view after a 5-second delay. This is useful for time-sensitive actions or redirects. ```dart ElevatedButton( onPressed: () => setState(() { _launched = _launchInWebViewOrVC(toLaunch); Timer(const Duration(seconds: 5), () { UrlLauncherPlatform.instance.closeWebView(); }); }), child: const Text('Launch in app + close after 5 seconds'), ), ``` -------------------------------- ### Make Phone Call Source: https://pub.dev/packages/url_launcher_android/example Initiates a phone call to a specified number. It's recommended to use `Uri` to properly encode the phone number, ensuring compatibility across different devices and locales. ```dart Future _makePhoneCall(String phoneNumber) async { // Use `Uri` to ensure that `phoneNumber` is properly URL-encoded. // Just using 'tel:$phoneNumber' would create invalid URLs in some cases. final launchUri = Uri(scheme: 'tel', path: phoneNumber); await launcher.launchUrl(launchUri.toString(), const LaunchOptions()); } ``` -------------------------------- ### Make Phone Call Source: https://pub.dev/packages/url_launcher/example Initiates a phone call to the specified phone number using the 'tel' scheme. ```dart Future _makePhoneCall(String phoneNumber) async { final Uri launchUri = Uri( scheme: 'tel', path: phoneNumber, ); await launchUrl(launchUri); } ``` -------------------------------- ### Launch URL in App with Custom Headers Source: https://pub.dev/packages/url_launcher/example Launches a URL within the application using an in-app web view, allowing for custom HTTP headers to be sent with the request. ```Dart final Uri toLaunch = Uri(scheme: 'https', host: 'www.cylog.org', path: 'headers/'); // ... inside a button's onPressed callback _launchAsInAppWebViewWithCustomHeaders(toLaunch) ```