为什么第三次点击按钮时发送消息崩溃?
Why sending message crashes when clicking button for third time?
我在我的项目中设置了向 phone 个号码发送消息并且工作正常。我点击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
}
}
}
我个人没有使用过 MFMessageComposeViewController
,但是看错误我可以猜测它可能不喜欢出现不止一次。您是否尝试过在要显示它时只创建 MFMessageComposeViewController
实例,而不是在内存中保留对它的引用并重新使用它?
我遇到了同样的问题。这对我有用。
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
messageVC = MFMessageComposeViewController()
}
我在我的项目中设置了向 phone 个号码发送消息并且工作正常。我点击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
}
}
}
我个人没有使用过 MFMessageComposeViewController
,但是看错误我可以猜测它可能不喜欢出现不止一次。您是否尝试过在要显示它时只创建 MFMessageComposeViewController
实例,而不是在内存中保留对它的引用并重新使用它?
我遇到了同样的问题。这对我有用。
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
messageVC = MFMessageComposeViewController()
}