码迷,mamicode.com
首页 > 移动开发 > 详细

iOS+Swift: 使用MessageUI.framework发送短信

时间:2014-10-28 19:37:10      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   ar   使用   div   on   log   

在iOS中, 可以使用MessageUI.framework框架发送短信, 步骤如下:

代码下载http://git.oschina.net/yao_yu/swift_cnblogs_samples/tree/master/Y0001_SendSMS

  1. 添加MessageUI.framework框架
  2. 为ViewController类

    • 添加UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate 接口
    • 实现func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult)
  3. 发送短信很简单

    func sendSMS(){
        if MFMessageComposeViewController.canSendText(){
            let controller = MFMessageComposeViewController()
            controller.body = txtSMS.text
            controller.recipients = txtTo.text.componentsSeparatedByString(",")
            controller.messageComposeDelegate = self
            self.presentViewController(controller, animated: true, completion: nil)
        } else {
            let alert = UIAlertView(title: "提示信息", message: "本设备不能发短信", delegate: nil, cancelButtonTitle: "确定")
            alert.show()
        }
    }
    
    func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
    
        controller.dismissViewControllerAnimated(true, completion: nil)
    
        switch result.value{
        case MessageComposeResultSent.value:
            lblResult.text = "短信已发送"
        case MessageComposeResultCancelled.value:
            lblResult.text = "短信已取消"
        case MessageComposeResultFailed.value:
            lblResult.text = "短信发送失败"
        default:
            break
        }
    }

iOS+Swift: 使用MessageUI.framework发送短信

标签:blog   http   io   os   ar   使用   div   on   log   

原文地址:http://www.cnblogs.com/yaoyu126/p/4057406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!