update sms plugin
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io' show Platform;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import 'user_agent/io.dart' if (dart.library.html) 'user_agent/web.dart';
|
||||
|
||||
const MethodChannel _channel = MethodChannel('flutter_sms');
|
||||
|
||||
class FlutterSmsPlatform extends PlatformInterface {
|
||||
/// Constructs a FlutterSmsPlatform.
|
||||
FlutterSmsPlatform() : super(token: _token);
|
||||
|
||||
static final Object _token = Object();
|
||||
|
||||
static FlutterSmsPlatform _instance = FlutterSmsPlatform();
|
||||
|
||||
/// The default instance of [FlutterSmsPlatform] to use.
|
||||
///
|
||||
/// Defaults to [MethodChannelFlutterSmsPlatform].
|
||||
static FlutterSmsPlatform get instance => _instance;
|
||||
|
||||
/// Platform-specific plugins should set this with their own platform-specific
|
||||
/// class that extends [FlutterSmsPlatform] when they register themselves.
|
||||
// TODO(amirh): Extract common platform interface logic.
|
||||
// https://github.com/flutter/flutter/issues/43368
|
||||
static set instance(FlutterSmsPlatform instance) {
|
||||
PlatformInterface.verifyToken(instance, _token);
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
///
|
||||
///
|
||||
Future<String> sendSMS({
|
||||
required String message,
|
||||
required List<String> recipients,
|
||||
bool sendDirect = false,
|
||||
}) {
|
||||
final mapData = <dynamic, dynamic>{};
|
||||
mapData['message'] = message;
|
||||
if (!kIsWeb && Platform.isIOS) {
|
||||
mapData['recipients'] = recipients;
|
||||
return _channel
|
||||
.invokeMethod<String>('sendSMS', mapData)
|
||||
.then((value) => value ?? 'Error sending sms');
|
||||
} else {
|
||||
String _phones = recipients.join(';');
|
||||
mapData['recipients'] = _phones;
|
||||
mapData['sendDirect'] = sendDirect;
|
||||
return _channel
|
||||
.invokeMethod<String>('sendSMS', mapData)
|
||||
.then((value) => value ?? 'Error sending sms');
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> canSendSMS() {
|
||||
return _channel
|
||||
.invokeMethod<bool>('canSendSMS')
|
||||
.then((value) => value ?? false);
|
||||
}
|
||||
|
||||
Future<bool> launchSmsMulti(List<String> numbers, [String? body]) {
|
||||
if (numbers.length == 1) {
|
||||
return launchSms(numbers.first, body);
|
||||
}
|
||||
String _phones = numbers.join(';');
|
||||
if (body != null) {
|
||||
final _body = Uri.encodeComponent(body);
|
||||
return launchUrl(
|
||||
Uri.parse('sms:/open?addresses=$_phones${separator}body=$_body'));
|
||||
}
|
||||
return launchUrl(Uri.parse('sms:/open?addresses=$_phones'));
|
||||
}
|
||||
|
||||
Future<bool> launchSms(String? number, [String? body]) {
|
||||
// ignore: parameter_assignments
|
||||
number ??= '';
|
||||
if (body != null) {
|
||||
final _body = Uri.encodeComponent(body);
|
||||
return launchUrl(Uri.parse('sms:/$number${separator}body=$_body'));
|
||||
}
|
||||
return launchUrl(Uri.parse('sms:/$number'));
|
||||
}
|
||||
|
||||
String get separator => isCupertino() ? '&' : '?';
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
bool isCupertino() {
|
||||
return Platform.isIOS || Platform.isMacOS;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:web/web.dart' as web;
|
||||
|
||||
bool isCupertino() {
|
||||
final _devices = [
|
||||
'iPad Simulator',
|
||||
'iPhone Simulator',
|
||||
'iPod Simulator',
|
||||
'iPad',
|
||||
'iPhone',
|
||||
'iPod',
|
||||
'Mac OS X',
|
||||
];
|
||||
final String _agent = web.window.navigator.userAgent;
|
||||
for (final device in _devices) {
|
||||
if (_agent.contains(device)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user