【问题标题】:Why sending message crashes when clicking button for third time?为什么第三次单击按钮时发送消息会崩溃?
【发布时间】:2016-10-19 14:32:37
【问题描述】:

我在我的项目中设置了向电话号码发送消息并且工作正常。我点击button,弹出iPhone发送消息页面,如果我发送消息或点击Cancel,它会返回。现在,如果我第二次单击该按钮,则没有任何反应,如果我第三次单击该按钮,则应用程序崩溃。控制台中的信息告诉我将 Use afterScreenUpdates:NO 更改为 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
    }
  }


 }

【问题讨论】:

    标签: swift mfmessagecomposeview messageui


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 似乎更像是评论而不是答案。但是一个 good 评论。 :)
      • @dlbuckley 猜得好!单击按钮时,我创建了 MFMessageComposeViewController 实例。现在它工作正常!谢谢。
      • 别担心!请记住,当您发现这样的错误时将它们提交给 Apple (bugreport.apple.com)。这是一个非常罕见的案例,但如果你发现了它,那么其他人也可能会发现它,所以最好让 Apple 知道,以便他们将来修复它。
      • @dlbuckley 是的,这非常罕见。我使用 imagepicker 和类似的东西,从来没有遇到过这个。将与苹果归档。好点。
      猜你喜欢
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多