【发布时间】:2017-01-02 20:44:28
【问题描述】:
我尝试创建的消息扩展应用程序存在问题。每当我尝试在对话中插入文本时,什么都没有发生。这就是我正在使用的:
import UIKit
import Messages
class MessagesViewController: MSMessagesAppViewController {
override func viewDidLoad() {
super.viewDidLoad()
let sendButton = UIButton(type: UIButtonType.custom)
sendButton.frame = CGRect(x: view.frame.midX, y: view.frame.midY, width: 100, height: 50)
sendButton.setTitle("Send", for: UIControlState.normal)
sendButton.addTarget(self, action: #selector(sendButtonTapped(sender:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(sendButton)
}
func sendButtonTapped(sender: UIButton!){
let layout = MSMessageTemplateLayout()
layout.caption = "My Survey"
layout.image = UIImage(named: "myImage")
let message = MSMessage()
message.layout = layout
self.activeConversation?.insert(message, completionHandler: nil)
}
}
每当我使用此代码运行它时,什么都不会发生。奇怪的是,如果我将self.activeConversation? 更改为self.activeConversation!,应用程序就会崩溃。我不明白为什么会发生这种情况,因为我正在对话中并且我的应用已加载。
【问题讨论】:
-
它可能崩溃了,因为 activeConversarion 为 nil 并且您尝试打开它。
-
@mat 但是为什么它会为零呢?应用程序已加载,我正在 iMessage 中进行对话。
-
什么是activeConversation?您发布的代码不足以确定崩溃的来源。控制台有错误吗?
-
@mat 它所说的只是“致命错误:在展开可选值时意外发现 nil”
-
这是意料之中的。您必须找出为什么 activeConversation 为零。我相信这是一个变量。你能发布更多代码吗?
标签: swift swift3 imessage imessage-extension