iOS调用系统发短信的两种方法

本文介绍了在Swift 3.0环境下,iOS应用调用系统发短信的两种方法:程序外调用和程序内调用。程序外调用简单直接,通过openURL实现;程序内调用则允许用户发送短信后返回应用,需要导入MessageUI.framework并设置相关参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS调用系统的发短信 功能可以分为两种:1、程序外调用系统发短信。2、程序内调用系统发短信。第二种的好处是用户发短信之后还可以回到APP。


1、程序外调用系统发短信(在swift3.0下)

这个方法其实很简单,直接调用openURL即可:

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"sms://13888888888"]];

2、程序内调用系统发短信(在swift3.0下)


1)导入MessageUI.framework,并引入头文件:

import MessageUI

2)实现代理方法MFMessageComposeViewControllerDelegate

    //MARK: //////////Delegate///////////
    //消息传送后的回调
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        self.dismiss(animated: true, completion: nil)
        switch result {
        case MessageComposeResult.sent:
            //信息传送成功
            break
        case MessageComposeResult.failed:
            //信息传送失败
            break
        case MessageComposeResult.cancelled:
            //信息被用户取消传送
            break
        }
    }


3)发送短信

//参数1:发送短信的电话号数组

//参数2:title为发短信页面标题(测试发现并无卵用,改页面标题仍然为“新信息”,如有人发现如何更改请告知)

//参数3:指定的发送内容

    //发送短信方法
    func showMessageComposeViewController(phones : NSArray, title : String, body: String){
        //判断当前设备是否有发短信功能
        if MFMessageComposeViewController.canSendText() {
            let controller = MFMessageComposeViewController()
            controller.recipients = phones as? [String]
            controller.navigationBar.tintColor = UIColor.red
            controller.body = body//短信内容
            controller.messageComposeDelegate = self//设置委托
            self.present(controller, animated: true, completion: nil)
            controller.viewControllers.last?.navigationItem.title = title
        } else{
            print("该设备部支持短信功能, 并作出相应的提醒")
        }
    }


4)调用发短信的方法

self.showMessageComposeViewController(phones: [], title: "test", body: "门店邀您参与分享活动标题,赶快来参与吧!http://baidu.com")



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值