为什么在第三次点击按钮时发送邮件崩溃?

时间:2016-10-19 14:32:38

标签: swift mfmessagecomposeview messageui

我设置了向项目中的电话号码发送消息并且工作正常。我点击button,弹出iPhone发送消息页面,如果我发送消息或点击Cancel,它会返回。现在,如果我第二次点击该按钮,没有任何反应,如果我第三次点击,应用程序崩溃。控制台中的信息告诉我更改Use afterScreenUpdates:NO to Use afterScreenUpdates:YES。所以我在代码中添加了controller.view.drawHierarchy(in: view.bounds, afterScreenUpdates: true),但它没有用。需要改变什么?

在控制台中:

Cannot snapshot view (<UIKeyboardImpl: 0x101a224f0; frame = (0 0; 320 216); layer = <CALayer: 0x170622880>>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES.

我的代码:

import UIKit
import MessageUI

class ViewController: UIViewController,  MFMessageComposeViewControllerDelegate {

let messageVC = MFMessageComposeViewController()

var phoneNumber = ""

override func viewDidLoad() {   
    super.viewDidLoad()

     messageVC.messageComposeDelegate = self
 }    

@IBAction func sendMessageTapped(_ sender: AnyObject) {

        let recipient = self.phoneNumber  // I get self.phonenumber from other code, no problem.

        messageVC.body = ""
        messageVC.recipients = [recipient]

    self.present(messageVC, animated: true, completion: nil)

}


func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
  // I added this line to fix, didn't work.   
//  controller.view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)


    switch result.rawValue {

    case 0 :

        print("Sending Message cancelled")

        messageVC.dismiss(animated: true, completion: nil)

    case 1:

        print("Message sent")
        messageVC.dismiss(animated: true, completion: nil)

    case 2:

        print("Sending message failed")
        messageVC.dismiss(animated: true, completion: nil)

    default:
        break
    }
  }


 }

2 个答案:

答案 0 :(得分:1)

我得到同样的问题。这对我有用。

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

答案 1 :(得分:0)

我还没有亲自使用过MFMessageComposeViewController,但是看错,我可以猜测它可能不会被多次出现。您是否尝试仅在显示MFMessageComposeViewController实例时创建它,而不是在内存中保留对它的引用并重新使用它?