MessageUI(发送短信)
来源:互联网 发布:mac自带抠图 编辑:程序博客网 时间:2024/06/05 22:59
在MessageUI库中发送短信使用MFMessageComposeViewController。
1 相关API
1.1 Determining If Message Composition Is Available
/// 能否发短信////// - returns: Boolpublic class func canSendText() -> Bool/// 能否发主题////// - returns: Bool@available(iOS 7.0, *)public class func canSendSubject() -> Bool/// 能否发附件////// - returns: Bool@available(iOS 7.0, *)public class func canSendAttachments() -> Bool/// 附件的UTI支持////// - parameter uti : See [Uniform Type Identifiers Reference](https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Introduction/Introduction.html)////// - returns: Bool@available(iOS 7.0, *)public class func isSupportedAttachmentUTI(uti: String) -> Bool
1.2 Accessing the Delegate
/// 监听发送结果unowned(unsafe) public var messageComposeDelegate: MFMessageComposeViewControllerDelegate?
1.3 Setting the Initial Message Information
/// 取消添加附件的按钮@available(iOS 7.0, *)public func disableUserAttachments()/// 收件人public var recipients: [String]?/// 内容public var body: String?/// 主题public var subject: String?/// 所有附件public var attachments: [[NSObject : AnyObject]]? { get }/// 添加url类型的附件@available(iOS 7.0, *)public func addAttachmentURL(attachmentURL: NSURL, withAlternateFilename alternateFilename: String?) -> Bool/// 添加NSData数据的附件@available(iOS 7.0, *)public func addAttachmentData(attachmentData: NSData, typeIdentifier uti: String, filename: String) -> Bool
2 实战演练
2.1 源代码
//// ViewController.swift// Message//// CSDN:http://blog.csdn.net/y550918116j// GitHub:https://github.com/937447974/Blog//// Created by yangjun on 16/1/11.// Copyright © 2016年 阳君. All rights reserved.//import UIKitimport MessageUIclass ViewController: UIViewController, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: compose message @IBAction func composeMessage(sender: AnyObject) { guard MFMessageComposeViewController.canSendText() else { print("不能发送短信") return } let messageVC = MFMessageComposeViewController() messageVC.messageComposeDelegate = self // 代理 messageVC.recipients = ["18511056826"] // 收件人 messageVC.body = "短信内容" // 内容 // 发送主题 if MFMessageComposeViewController.canSendSubject() { messageVC.subject = "阳君" } // 发送附件 if MFMessageComposeViewController.canSendAttachments() { // 路径添加 if let path = NSBundle.mainBundle().pathForResource("Info", ofType: "plist") { messageVC.addAttachmentURL(NSURL(fileURLWithPath: path), withAlternateFilename: "Info.plist") } // NSData添加 if MFMessageComposeViewController.isSupportedAttachmentUTI("public.png") { if let image = UIImage(named: "qq") { if let data = UIImagePNGRepresentation(image) { // 添加文件 messageVC.addAttachmentData(data, typeIdentifier: "public.png", filename: "qq.png") } } } print(messageVC.attachments) // 所有附件 } // messageVC.disableUserAttachments() // 禁用添加附件按钮 self.presentViewController(messageVC, animated: true, completion: nil) } // MARK: - MFMessageComposeViewControllerDelegate func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) { // 关闭MFMessageComposeViewController controller.dismissViewControllerAnimated(true, completion: nil) switch result { // 发送状态 case MessageComposeResultCancelled: print("Result: Mail sending cancelled") // 取消发送 case MessageComposeResultSent: // 发送成功 print("Result: Mail sent") case MessageComposeResultFailed: // 发送失败 print("Result: Message sending failed") default:// 其他 print("Result: Message not sent") } }}
2.2 效果图
Appendix
Sample Code
Swift
Related Documentation
Message UI Framework Reference
Revision History
Copyright
CSDN:http://blog.csdn.net/y550918116j
GitHub:https://github.com/937447974/Blog
0 0
- MessageUI(发送短信)
- iOS中发送短信message------MessageUI框架
- <MessageUI/MessageUI.h>发送邮件
- MessageUI框架发短信
- MessageUI.framework发短信
- MessageUI发送邮件
- MessageUI(发送邮件)
- IOS疯狂基础之MessageUI—短信
- IOS疯狂基础之MessageUI—短信
- 【MessageUI】iOS调用 短信 与 邮件
- IOS使用MessageUI Framework 发送短信息
- IOS使用MessageUI Framework发送短信息
- iOS开发:IOS使用MessageUI Framework 发送短信息
- 利用Mono Touch的MessageUI组件发送email
- IOS怎样使用MessageUI Framework 发送短信息
- 发送短信
- 发送短信
- 发送短信
- fragment调用方法isadded()、isvisible()
- Android studio AIDL详解
- CSS Media Query
- 广搜与优先队列-----POJ2312
- laravel5中间件的使用例子
- MessageUI(发送短信)
- 嵌入式启动定时任务代码
- 实体类中创建数据表中不存在的字段
- mysql登陆问题
- 让PHP7达到最高性能的几个Tips
- 将博客搬至CSDN
- 网址纪录
- android自动弹出软键盘(输入键盘)
- Linux安装keepalived