Flutter url_launcher调用iOS iMessage无法加载号码及内容问题
Fix for url_launcher SMS Issue on iOS
Root Causes
- iOS URI Format Difference: Unlike Android, iOS requires the recipient number to be passed via the
toquery parameter instead of the URI path. - Double Encoding: Manually using
Uri.encodeComponenton the body text while usingqueryParametersresults in double encoding, which breaks the message content. - Missing Async/Await: Not awaiting
launchUrlcan lead to race conditions or unexpected behavior.
Corrected Code
First, import dart:io to detect the platform:
import 'dart:io'; import 'package:url_launcher/url_launcher.dart';
Adjust the button click handler to use platform-specific URI construction:
Future<void> sendSMS() async { const String phoneNumber = '3939393939'; const String message = 'Example Subject & Symbols are allowed!'; Uri smsLaunchUri; if (Platform.isIOS) { // iOS uses 'to' query parameter for recipient smsLaunchUri = Uri( scheme: 'sms', queryParameters: { 'to': phoneNumber, 'body': message, }, ); } else { // Android uses path for recipient smsLaunchUri = Uri( scheme: 'sms', path: phoneNumber, queryParameters: { 'body': message, }, ); } if (await canLaunchUrl(smsLaunchUri)) { await launchUrl(smsLaunchUri); } else { throw 'Could not launch SMS'; } }
Additional Notes
- Testing on Real Device: iOS Simulator has limited support for SMS functionality. For reliable testing, use a physical iOS device.
- Info.plist Configuration: Your existing
LSApplicationQueriesSchemessetup is correct and doesn't need changes.
内容的提问来源于stack exchange,提问作者watercroc
相关产品推荐
相关产品推荐

