1

我尝试创建的消息扩展应用程序有问题。每当我尝试在对话中插入文本时,什么都没有发生。这就是我正在使用的:

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!应用程序崩溃。我不明白为什么会发生这种情况,因为我正在对话并且我的应用程序已加载。

4

1 回答 1

0

你能试试这个。

func sendButtonTapped(发件人:UIButton!){

guard let conversation = activeConversation else { fatalError("Expected a  conversation") }

        let session = conversation.selectedMessage?.session ?? MSSession()

        let message = MSMessage(session: session)

        let layout = MSMessageTemplateLayout()
        layout.image = createImageForMessage(value: GlobalData.sharedManager.cardUrl)
        layout.caption = "Caption"
        layout.imageTitle = GlobalData.sharedManager.cardTitle
        layout.imageSubtitle = "Image subtitle"
        layout.trailingCaption = "Trailing caption"
        layout.subcaption = "Subcaption"
        layout.trailingSubcaption = "Trailing subcaption"
        message.layout = layout

        conversation.insert(message) {error in
                  // empty for now
              }
}}
于 2017-01-02T12:44:28.807 回答