1.iOS 发送短信功能

1.iOS 发送短信功能

iOS常见的发送短信方式有openURLMessageUI两种方式,第一种比较过时,需要跳转到短信App进行发送,第二种允许在App进行发送短信,并且能够取到短信发送状态

1.openURL方式

UIApplication.shared.open(URL(string: "sms://\(number)")!, options: [:], completionHandler: nil)

2.MessageUI 方式

使用这个方式需要导入MessageUI框架

if MFMessageComposeViewController.canSendText() {
    let vc = MFMessageComposeViewController()
    vc.recipients = [number] // 支持多个手机号
    vc.body = "今天晚上有空么,一起吃个饭" // 支持文字直接进入文本框
    vc.messageComposeDelegate = self
    self.present(vc, animated: true, completion: nil)
}

2.1 MFMessageComposeViewControllerDelegate

/// 发送短信回调
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    controller.dismiss(animated: true, completion: nil);
    
    switch result {
    case .cancelled:
        print("短信发送 -- 取消")
    case .sent:
        print("短信发送 -- 成功")
    case .failed:
        print("短信发送-- 失败")
    default:
        break
    }
}

3.代码地址

小小个子大个头

posted @   小小个子大个头  阅读(745)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示