情诗网 >情话短信 > 正文

Swift 短信分享

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

功能:用于短信分享内容

    //MARK: 短信
    // 调用方法
    func sendMessage() {
       if (self.canSendText()) {
            self.presentViewController(self.configuredMessageComposeViewController(), animated: true, completion: nil)
       } else {
            MPAlertView.showAlertView("当前设备不支持短信功能")
       }
    }
    
    // 是否能发送短信
    func canSendText() -> Bool {
        return MFMessageComposeViewController.canSendText()
    }
    
    func configuredMessageComposeViewController() -> MFMessageComposeViewController {
        let messageComposeVC = MFMessageComposeViewController()
        messageComposeVC.messageComposeDelegate = self // 设置代理,遵循代理方法
        let inviteCode = NSUserDefaults.standardUserDefaults().stringForKey(kInviteCode)
        messageComposeVC.body = "短信内容"
        return messageComposeVC
    }
    
    //MARK: MFMessageComposeViewControllerDelegate
    func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
        switch result.value {
        case 0:
            MPAlertView.showAlertView("取消发送")
            self.dismissViewControllerAnimated(true, completion: nil)
            
        case 1:
            MPAlertView.showAlertView("发送失败")
            self.dismissViewControllerAnimated(true, completion: nil)

        case 2:
            MPAlertView.showAlertView("发送成功")
            self.dismissViewControllerAnimated(true, completion: nil)

        default:
            controller.dismissViewControllerAnimated(true, completion: nil)
        }
        self.dismissViewControllerAnimated(true, completion: nil)
    }

热门文章