草庐IT

iOS10 iMessage : Unable to insert data into iMessage using MSConversation

coder 2023-09-15 原文

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

声明

 var savedConversation: MSConversation?

我想在用户 didselectRow - Tableview 时发送文本,但它不会抛出任何错误,甚至不会转到 MSConversation 完成 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?start=0&tstart=0

最佳答案

这段代码对我有用

第一步

创建 iMessage 扩展

第二步

转到 MessagesViewController(由扩展创建)

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
}

检查此委托(delegate)是否收到消息

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 : Unable to insert data into iMessage using MSConversation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39571141/

有关iOS10 iMessage : Unable to insert data into iMessage using MSConversation的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  5. 由于 libgmp.10.dylib 的问题,Ruby 2.2.0 无法运行 - 2

    我刚刚安装了带有RVM的Ruby2.2.0,并尝试使用它得到了这个:$rvmuse2.2.0--defaultUsing/Users/brandon/.rvm/gems/ruby-2.2.0dyld:Librarynotloaded:/usr/local/lib/libgmp.10.dylibReferencedfrom:/Users/brandon/.rvm/rubies/ruby-2.2.0/bin/rubyReason:Incompatiblelibraryversion:rubyrequiresversion13.0.0orlater,butlibgmp.10.dylibpro

  6. ruby - ri 有空文件 – Ubuntu 11.10, Ruby 1.9 - 2

    我正在运行Ubuntu11.10并像这样安装Ruby1.9:$sudoapt-getinstallruby1.9rubygems一切都运行良好,但ri似乎有空文档。ri告诉我文档是空的,我必须安装它们。我执行此操作是因为我读到它会有所帮助:$rdoc--all--ri现在,当我尝试打开任何文档时:$riArrayNothingknownaboutArray我搜索的其他所有内容都是一样的。 最佳答案 这个呢?apt-getinstallri1.8编辑或者试试这个:(非rvm)geminstallrdocrdoc-datardoc-da

  7. ruby-on-rails - gem install rmagick -v 2.13.1 错误 Failed to build gem native extension on Mac OS 10.9.1 - 2

    我已经通过提供MagickWand.h的路径尝试了一切,我安装了命令工具。谁能帮帮我?$geminstallrmagick-v2.13.1Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrmagick:ERROR:Failedtobuildgemnativeextension./Users/ghazanfarali/.rvm/rubies/ruby-1.8.7-p357/bin/rubyextconf.rbcheckingforRubyversion>=1.8.5...yescheckingfor/

  8. ruby - 安装 tiny_tds 在 mac os 10.10.5 上出现错误 - 2

    我正在使用macos,我想使用ruby​​驱动程序连接到sqlserver。我想使用tiny_tds,但它给出了缺少free_tds的错误,但它已经安装了。怎么能过这个?~brewinstallfreetdsWarning:freetds-0.91.112alreadyinstalled~sudogeminstalltiny_tdsBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtiny_tds:ERROR:Failedtobuildgemnativeextension.完整日志如下:/System

  9. ruby - rails 3.2.2(或 3.2.1)+ Postgresql 9.1.3 + Ubuntu 11.10 连接错误 - 2

    我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat

  10. ruby-on-rails - 在 osx 10.9.3 上使用 RVM 安装 ruby​​-1.9.3-p547 时出错 - 2

    如何解决这个错误:$rvminstall1.9.3Searchingforbinaryrubies,thismighttakesometime.Nobinaryrubiesavailablefor:osx/10.9/x86_64/ruby-1.9.3-p547.Continuingwithcompilation.Pleaseread'rvmhelpmount'togetmoreinformationonbinaryrubies.Checkingrequirementsforosx.Certificatesin'/usr/local/etc/openssl/cert.pem'arealr

随机推荐