最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - launchUrl Not Working with LaunchMode.externalApplication on iOS - Stack Overflow

programmeradmin4浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论