admin管理员组

文章数量:1410682

I am trying to launch URLs, emails, phone calls, and SMS using the url_launcher package in Flutter. While it works fine on Android, it does not work as expected on iOS when using LaunchMode.externalApplication.

  • Flutter : 3.19.6
  • url_launcher: 6.3.1

I have a custom function to handle different URL types:

import 'package:url_launcher/url_launcher.dart';

Future<bool> openLink({
  required String url,
  LaunchMode? mode,
  required PrefixLauncher prefix,
  bool showMsg = true,
  WebViewConfiguration? webViewConfiguration,
}) async {
  try {
    var baseUrl = '';
    switch (prefix) {
      case PrefixLauncher.url:
        baseUrl = 'https://$url';
        break;
      case PrefixLauncher.email:
        baseUrl = 'mailto:$url';
        break;
      case PrefixLauncher.call:
        baseUrl = 'tel:$url'.replaceAll(" ", "");
        break;
      case PrefixLauncher.message:
        baseUrl = 'sms:$url';
        break;
      default:
        baseUrl = url;
        break;
    }

    Uri uri = Uri.parse(baseUrl);

    if (await canLaunchUrl(uri)) {
      if (!await launchUrl(
        uri,
        mode: mode ?? LaunchMode.externalApplication, // Default mode
      )) {
        if (showMsg) showToast('something_wrong'.tr());
        Log.error('Could not launch : $url');
        return false;
      } else {
        return true;
      }
    }
    return false;
  } on Exception catch (e) {
    Log.error('Error: $e');
    return false;
  }
}

I have added the required schemes in my ios/Runner/Info.plist file:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>https</string>                                                     
    <string>http</string>
    <string>mailto</string>
    <string>tel</string>
    <string>sms</string>
</array>

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

The app should open URLs, email clients, phone dialer, and SMS apps externally on iOS when using LaunchMode.externalApplication.

However, on iOS, calling openLink() with LaunchMode.externalApplication does not launch the external app. No error message is displayed. It works fine on Android.

本文标签: flutterlaunchUrl Not Working with LaunchModeexternalApplication on iOSStack Overflow