iOS10 iMessage:无法使用MSConversation将数据插入到iMessage中
在我的项目中,我添加了iMessage Extension
但我无法从UITableview
发送选定的数据(需要发送选定的行数据)
声明
var savedConversation: MSConversation?
我想发送文本时,用户didselectRow
– didselectRow
,但它没有抛出任何错误,甚至没有去MSConversation
完成块
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let message = MSMessage() let layout = MSMessageTemplateLayout() layout.caption = "Win Notification"//post.title layout.subcaption = "Can share now through win" layout.trailingSubcaption = "share" message.url = "http://.." message.layout = layout savedConversation?.insert(message, completionHandler: { (error) in if error == nil { //error nothing so do something }else{ print("error in inserting -- \(error)") } })
推荐链接https://www.captechconsulting.com/blogs/ios-10-imessages-sdk-creating-an-imessages-extension
https://discussions.apple.com/thread/7612113?start=0&tstart=0
这段代码适用于我
步骤1
创buildiMessage扩展
第2步
转到MessagesViewController(由Extension创build)
class MessagesViewController: MSMessagesAppViewController { // ****This code written just for your sample reference******** override func viewDidLoad() { let button = UIButton(type: .system) button.sizeToFit() view.addSubview(button) loginButton.setTitle("Click to Insert Message!", for: []) loginButton.addTarget(self, action: #selector(sendMessage(_:)), for: .touchUpInside) } }
将消息插入iMessage
func sendMessage(_ sender:UIButton) { let message = composeMessage(customMessage: "Too good!!! & Easy", caption: "Notify:", subCaption:"Caption", trailingCaptionn: "Subcaption", trailingSubcaptionn: "trailing sub caption") self.activeConversation?.insert(message) // Inserts Message }
用可用选项创build消息[此方法返回消息]
func composeMessage(customMessage: String, caption: String, subCaptionn: String, trailingCaptionn: String, trailingSubcaptionn: String) -> MSMessage { var components = URLComponents() // create a query item from our custom message let item = URLQueryItem(name: "MyCustomMessage", value: customMessage) // put it into an array of query items var items = [URLQueryItem]() items.append(item) components.queryItems = items // tell messages to use the default message template layout let layout = MSMessageTemplateLayout() layout.caption = caption layout.subcaption = subCaptionn layout.trailingCaption = trailingCaptionn layout.trailingSubcaption = trailingSubcaptionn // create a message and tell it the content and layout let message = MSMessage() message.url = components.url! message.layout = layout // return it for sending return message }
检查此代表是否收到消息
override func didReceive(_ message: MSMessage, conversation: MSConversation) { // Called when a message arrives that was generated by another instance of this // extension on a remote device. // Use this method to trigger UI updates in response to the message. guard let messageURL = message.url else { return } guard let urlComponents = NSURLComponents(url: messageURL, resolvingAgainstBaseURL: false), let queryItems = urlComponents.queryItems else { return } print("URL Components", urlComponents) print("queryItems", queryItems) for item in queryItems { print("Received \(item.name) with value \(item.value)") } } }
参考&帮助来源: https : //www.hackingwithswift.com/ios10