gpt4 book ai didi

swift - iMessage 应用程序不会将消息插入事件对话

转载 作者:行者123 更新时间:2023-11-28 08:20:39 26 4
gpt4 key购买 nike

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

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

最佳答案

你能试试这个吗?

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
}
}}

关于swift - iMessage 应用程序不会将消息插入事件对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41399735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com