swift发短信
import MessageUI
// MARK: - composeMail
privatefunccomposeMail(){
guard MFMessageComposeViewController.canSendText() else {
print("不能发送短信")
return
}
let messageVC = MFMessageComposeViewController()
messageVC.messageComposeDelegate = self // 代理
messageVC.recipients= ["18346400992"]// 收件人
messageVC.body="美好生活"// 内容
// 发送主题
if MFMessageComposeViewController.canSendSubject() {
messageVC.subject="年"
}
// 发送附件
if MFMessageComposeViewController.canSendAttachments() {
// 路径添加
ifletpath =Bundle.main.path(forResource:"Info", ofType:"plist") {
messageVC.addAttachmentURL(URL(fileURLWithPath: path), withAlternateFilename:"Info.plist")
}
// NSData添加
if MFMessageComposeViewController.isSupportedAttachmentUTI("public.png") {
// See [Uniform Type Identifiers Reference](https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Introduction/Introduction.html)
ifletimage =UIImage(named:"bottom_logo") {
ifletdata =UIImagePNGRepresentation(image) {
// 添加文件
messageVC.addAttachmentData(data, typeIdentifier:"public.png", filename:"bottom_logo.png")
}
}
}
}
// messageVC.disableUserAttachments() // 禁用添加附件按钮
self.present(messageVC, animated:true, completion:nil)
}
// MARK: - MFMessageComposeViewControllerDelegate
funcmessageComposeViewController(_controller:MFMessageComposeViewController, didFinishWith result:MessageComposeResult) {
print(controller.attachmentsasAny)// 所有附件
// 关闭MFMessageComposeViewController
controller.dismiss(animated:true, completion:nil)
switchresult {// 发送状态
caseMessageComposeResult.cancelled:
print("Result: Mail sending cancelled") // 取消发送
caseMessageComposeResult.sent:// 发送成功
print("Result: Mail sent")
caseMessageComposeResult.failed:// 发送失败
print("Result: Message sending failed")
}
}