本文介绍了iOS10 iMessage:无法使用 MSConversation 将数据插入 iMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我添加了 iMessage Extension 但我无法从 UITableview 发送选定的数据(需要发送选定的行数据)

In my project i added iMessage Extension but i am unable to send selected data from UITableview (need to send selected row data)

声明

 var savedConversation: MSConversation?

我想在用户 didselectRow - Tableview 时发送文本,但它没有抛出任何错误,甚至没有进入 MSConversation 完成块

I want to send the text when user didselectRow - Tableview but it is not throwing any error and not even going to MSConversation completion Block

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?开始=0&tstart=0

推荐答案

这段代码对我有用

步骤 1

创建 iMessage 扩展

This code works for me

Step1

Create iMessage Extension

转到 MessagesViewController(由扩展创建)

Go to MessagesViewController(created by Extension)

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
}

使用可用选项创建消息[此方法返回消息]

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

这篇关于iOS10 iMessage:无法使用 MSConversation 将数据插入 iMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:52