SendsmsPlugin.m 1.61 KB
#import "SendsmsPlugin.h"

@implementation SendsmsPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"sendsms"
            binaryMessenger:[registrar messenger]];
  SendsmsPlugin* instance = [[SendsmsPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }else if([@"sendSMS" isEqualToString:call.method]){
     NSString *phone = call.arguments[@"phoneNumber"];
     NSString *content = call.arguments[@"message"];
    // 短信号码可以为空
    NSString *urlStr = [NSString stringWithFormat:@"sms://%@&body=%@", phone, content];
    // 对中文进行编码 不进行中文编码,可能到时短信内容为null
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlStr];
    
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:url];
    }
  }else if([@"getPermission" isEqualToString:call.method]) {

//      result(true);
  }else if([@"hasPermission" isEqualToString:call.method]){ 
//      result(true);
  }
  else {
    result(FlutterMethodNotImplemented);
  }
}

@end