Swift2.2 发送短信

作者: 果啤 | 来源:发表于2016-05-16 17:20 被阅读442次

    代码如下

    <pre>
    <code>
    `
    import UIKit
    import MessageUI//*

    class ViewController: UIViewController ,
    MFMessageComposeViewControllerDelegate//*
    {
    override func viewDidLoad()
    {
    super.viewDidLoad()

        //首先要判断设备具不具备发送短信功能
        if MFMessageComposeViewController.canSendText()
        {
            let controller = MFMessageComposeViewController()
            //设置短信内容
            controller.body = "短信内容:快交话费!!!"
            //设置代理
            controller.messageComposeDelegate = self
            //打开界面
            self.presentViewController(controller, animated: true, completion: { () -> Void in
                
            })
        }
        else
        {
            print("本设备不能发送短信")
        }
    }
    
    //发送短信结束后调用此代理方法
    func messageComposeViewController(controller: MFMessageComposeViewController,
        didFinishWithResult result: MessageComposeResult)
    {
            controller.dismissViewControllerAnimated(true, completion: nil)
    

    // switch result.value
    switch result.rawValue

            {
            case MessageComposeResultSent.rawValue:
                print("短信已发送")
            case MessageComposeResultCancelled.rawValue:
                print("短信取消发送")
            case MessageComposeResultFailed.rawValue:
                print("短信发送失败")
            default:
                break
            }
    }
    
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
    

    }

    `
    </code>
    </pre>

    效果如下

    Swift2.2 发送短信

    相关文章

      网友评论

        本文标题:Swift2.2 发送短信

        本文链接:https://www.haomeiwen.com/subject/givbrttx.html