情诗网 >情话短信 > 正文

Swift2.2 发送短信

来源:情诗网    2021-02-16    分类:情话短信


代码如下

<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>

效果如下

热门文章