iOS获取系统短信,发送指定短信内容

现在在开发中应该会经常遇到点击邀请 发送短信给指定人员
这里写图片描述
点击邀请 弹出短信界面 并发送指定内容,手动点击发送即可

如果只要打开短信界面,并显示号码很简单,一句代码

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@sms://13666666666]];

但要指定短信内容 需要如下方法
1.先引入头文件

#import <MessageUI/MFMessageComposeViewController.h>

2.设置代理

MFMessageComposeViewControllerDelegate

3.实现代理方法

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self.mobileContactsVC dismissViewControllerAnimated:YES completion:nil];

    if (result == MessageComposeResultCancelled)
    {
        NSLog(@"Message cancelled");
    }
    else if (result == MessageComposeResultSent)
    {
        NSLog(@"Message sent");
    }
    else
    {
        NSLog(@"Message failed");
    }
}
  1. 调用方法
/**
 *  指定内容 打开SMS
 *
 *  @param bodyOfMessage 要发送的内容
 *  @param recipients    要发送的号码
 */
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

    if([MFMessageComposeViewController canSendText])

    {
        controller.body = bodyOfMessage;

        controller.recipients = recipients;

        controller.messageComposeDelegate = self;

        [self.mobileContactsVC presentViewController:controller animated:YES completion:nil];

    }   

}```

只要在需要调用的地方 调用方法即可

[self sendSMS:@"加入工付宝 领取红包金额" recipientList:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@", self.mobileContactsModel.mobile], nil]];
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

建古

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

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

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

打赏作者

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

抵扣说明:

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

余额充值