通过短信快速推荐朋友4

时间:2019-03-12 15:32:21

标签: ios swift sms refer

您好,想通过SMS推荐朋友,我编写了以下代码。如果选择了该用户,则打开带有文本的SMS,但不会再次取消,用户将无法返回到应用程序。

if indexPath.item == 1
        {
            //SMS
            if MFMessageComposeViewController.canSendText() {

                let urlToShare = self.referalmodeldata[0].referralCodeOnly

                controller1.body = "Hey I just gave an Awesome Assessment on App you can also try it. I scored , Try to beat my score \(String(describing: urlToShare))"

                controller1.messageComposeDelegate = self as? MFMessageComposeViewControllerDelegate
                self.present(controller1, animated: true, completion: nil)
            }
        }

1 个答案:

答案 0 :(得分:0)

您未在代码中正确实现MFMessageComposeViewControllerDelegate

首先,您需要通过MFMessageComposeViewControllerDelegate确认UIViewController,如下所示:

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

接下来的事情是您需要添加它的委托方法,如下所示:

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    self.dismiss(animated: true, completion: nil)
}

然后您需要替换

controller1.messageComposeDelegate = self as? MFMessageComposeViewControllerDelegate

controller1.messageComposeDelegate = self

演示代码为:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

    @IBAction func sendText(_ sender: Any) {
        if (MFMessageComposeViewController.canSendText()) {
            let controller = MFMessageComposeViewController()
            controller.body = "Message Body"
            controller.messageComposeDelegate = self
            self.present(controller, animated: true, completion: nil)
        }
    }

    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        self.dismiss(animated: true, completion: nil)
    }
}