iOS开发SwiftSwift专题iOS开发技术分享

Swift 短信分享

2015-10-26  本文已影响362人  devchena

功能:用于短信分享内容

    //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)
    }

上一篇下一篇

猜你喜欢

热点阅读