iOS MessageUI 方式发送多人短信息

iOS 发送短信有两种方法

方法一:

此方法有个缺点,iOS原生调用可以做短信多人发送,多账号之间用英文逗号","隔开
问题: 遇到js调用,即使是插件调用下面代码,只能识别第一个账号。逗号不被识别,导致多个账号不能被分开识别,打开短信息页面时只有一个长的号码
所以,推荐使用第二种方法

    NSString* msg = [@"sms:13551,138450&body=This is content" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL* url = [NSURL URLWithString:msg];
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
        
    }];

方法二

此方法依赖 MessageUI 框架使用,可以到短信息的单发、群发,是Apple官方推荐使用方法

代码如下:

//
//  SmsModule.h
//  Nick
//
//  Created by Nick5683 on 2022/7/19.
//  Copyright © 2020 Nick5683. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>

@interface SmsModule : NSObject <MFMessageComposeViewControllerDelegate>


- (void)sendMessage:(NSString*)message recipients:(NSArray*)recipients;

@end

//
//  SmsModule.m
//  Nick
//
//  Created by Nick5683 on 2022/7/19.
//  Copyright © 2020 Nick5683. All rights reserved.
//

#import "SmsModule.h"

@implementation SmsModule

- (void)sendMessage:(NSString*)message recipients:(NSArray*)recipients;
        
        if (![MFMessageComposeViewController canSendText]) {

            dispatch_async(dispatch_get_main_queue(void), ^{
                NSString *errorMessage = @"Sms Text not available.";

                UIAlertController * alert = [UIAlertController
                                             alertControllerWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]
                                             message:errorMessage
                                             preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *ok = [
                                    UIAlertAction actionWithTitle:@"OK"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction *action){

                                        }
                                    ];
                [alert addAction:ok];
                [currentViewController() presentViewController:alert animated:YES completion:nil];
            });
            return;
        }
        
        dispatch_async(dispatch_get_main_queue(void), ^{
            MFMessageComposeViewController *composeViewController = [[MFMessageComposeViewController alloc] init];
            composeViewController.messageComposeDelegate = self;
        
            [composeViewController setBody:message];
            if (recipients != nil) {
                if ([recipients.firstObject isEqual: @""]) {
                    [recipients replaceObjectAtIndex:0 withObject:@"?"];
                }

                [composeViewController setRecipients:recipients];
            }
            
            // code to be executed on the main queue after delay
            [self.viewController presentViewController:composeViewController animated:YES completion:nil];

        });
}

#pragma mark - MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

    NSString* message = @"";
    
    switch(result) {
        case MessageComposeResultCancelled:
            message = @"Message cancelled.";
            break;
        case MessageComposeResultSent:
            message = @"Message sent.";
            break;
        case MessageComposeResultFailed:
            message = @"Message failed.";
            break;
        default:
            message = @"Unknown error.";
            break;
    }
    
    [self.viewController dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"----------%@",message);
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nick5683

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值