Swift3.0 中實(shí)現(xiàn)發(fā)短信功能

公司項(xiàng)目中要用到點(diǎn)擊按鈕發(fā)短信的功能,由于之前沒(méi)做過(guò),在網(wǎng)上找了一些 demo, 自己總結(jié)點(diǎn),大神勿噴 -

**要實(shí)現(xiàn)發(fā)短信的功能其實(shí)不難,可以利用系統(tǒng)的MessageUI.framework框架 **就可以實(shí)現(xiàn),步驟如下:
  1.首先判斷設(shè)備是否有發(fā)送短信功能
  2.如果設(shè)備允許發(fā)送短信,創(chuàng)建一個(gè)MFMessageComposeViewController視圖控制器,并對(duì)其設(shè)置短信內(nèi)容和收件人號(hào)碼列表。
  3.短信發(fā)送后,會(huì)執(zhí)行回調(diào)代理方法,可以獲取發(fā)送結(jié)果(成功,失敗或取消)
代碼如下:

import UIKit
//導(dǎo)入頭文件
import MessageUI

//遵守代理 - MFMessageComposeViewControllerDelegate
class ViewController: UIViewController , UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
      self.view.backgroundColor = UIColor.lightGray
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      //設(shè)置聯(lián)系人
        let str = "10086"
      //創(chuàng)建一個(gè)彈出框提示用戶
        let alertController = UIAlertController(title: "發(fā)短信", message: "是否給\(str)發(fā)送短信?", preferredStyle: .alert)
        let cancleAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let sendAction = UIAlertAction(title: "確定", style: .default) { (alertController) in
            //判斷設(shè)備是否能發(fā)短信(真機(jī)還是模擬器)
            if MFMessageComposeViewController.canSendText() {
                let controller = MFMessageComposeViewController()
                //短信的內(nèi)容,可以不設(shè)置
                controller.body = "發(fā)短信"
                //聯(lián)系人列表
                controller.recipients = [str]
                //設(shè)置代理
                controller.messageComposeDelegate = self
                self.present(controller, animated: true, completion: nil)
            } else {
                print("本設(shè)備不能發(fā)短信")
            }
        }
        alertController.addAction(cancleAction)
        alertController.addAction(sendAction)
        
        self.present(alertController, animated: true, completion: nil)
        
    }
    //實(shí)現(xiàn)MFMessageComposeViewControllerDelegate的代理方法
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true, completion: nil)
        //判斷短信的狀態(tài)
        switch result{

        case .sent:
            print("短信已發(fā)送")
        case .cancelled:
            print("短信取消發(fā)送")
        case .failed:
            print("短信發(fā)送失敗")
        default:
            print("短信已發(fā)送")
            break
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

這樣一個(gè)簡(jiǎn)單的發(fā)短信的功能就實(shí)現(xiàn)了,基本上夠用了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容