【问题标题】:ios 11 imessage extension message.url does not open safariios 11 imessage 扩展 message.url 无法打开 safari
【发布时间】:2017-09-22 15:21:30
【问题描述】:

我正在向我的应用程序添加一个iMessage extension 目标。该扩展应该发送一条具有url 属性的消息。当用户触摸消息时,我期望的行为是使用消息的url 属性打开浏览器。

我的messageView 中有一个执行此代码的按钮:

@IBAction func labelButton(_ sender: Any) {
        let layout = MSMessageTemplateLayout()
        layout.imageTitle = "iMessage Extension"
        layout.caption = "Hello world!"
        layout.subcaption = "Test sub"
        guard let url: URL = URL(string: "https://google.com") else { return }

        let message = MSMessage()
        message.layout = layout
        message.summaryText = "Sent Hello World message"
        message.url = url

        activeConversation?.insert(message, completionHandler: nil)
    }

如果我触摸消息,它会扩展 MessageViewController

然后我添加了这个:

override func didSelect(_ message: MSMessage, conversation: MSConversation) {

        if let message = conversation.selectedMessage {
            // message selected

            // Eg. open your app:
            self.extensionContext?.open(message.url!, completionHandler: nil)
        }
}

现在,当我触摸消息时,它会打开我的主应用程序,但仍然不能打开我的浏览器。

我在another post 上看到(我无法发表评论,因此我打开了这篇文章)在Safari 中无法打开,但我有一个news app 插入文章链接并允许单击安装应用时在浏览器窗口中打开文章的消息。

那么,谁能告诉我如何继续在浏览器窗口中强制打开链接?

非常感谢。

【问题讨论】:

  • 您可以使用SFSafariViewController。正如另一篇文章指出的那样,您不能将用户从消息扩展中踢出到 Safari。
  • 感谢您的回答。如果我没有在浏览器中打开窗口的新闻应用程序,我会考虑其他帖子中的答案。

标签: ios swift safari imessage-extension


【解决方案1】:

这是在消息中插入链接的技巧。它不允许创建具有 url 属性的对象,而只允许直接插入将在 Safari 中打开的链接。

activeConversation?.insertText("https://google.com", completionHandler: nil)

【讨论】:

    【解决方案2】:

    我发布了一个sample on github,展示了如何从 iMessage 扩展程序中启动 URL。它只是使用一个固定的 URL,但启动代码是你需要的。

    复制自我的readme

    显而易见的尝试是self.extensionContext.open,即documented,因为要求系统代表当前运行的应用扩展打开一个URL。

    这行不通。但是,您可以迭代备份响应程序链,为 open 方法(实际上是 iMessage 实例)找到合适的处理程序,并使用该对象调用 open

    此方法适用于将打开本地应用程序的 URL,例如相机设置或 Web URL。

    code

    @IBAction public func onOpenWeb(_ sender: UIButton)  {
        guard let url = testUrl else {return}
    
        // technique that works rather than self.extensionContext.open
        var responder = self as UIResponder?
        let handler = { (success:Bool) -> () in
            if success {
                os_log("Finished opening URL")
            } else {
                os_log("Failed to open URL")
            }
        }
    
        let openSel = #selector(UIApplication.open(_:options:completionHandler:))
        while (responder != nil){
            if responder?.responds(to: openSel ) == true{
                // cannot package up multiple args to openSel so we explicitly call it on the iMessage application instance
                // found by iterating up the chain
                (responder as? UIApplication)?.open(url, completionHandler:handler)  // perform(openSel, with: url)
                return
            }
            responder = responder!.next
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-14
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 2017-10-24
      • 2017-04-19
      • 1970-01-01
      相关资源
      最近更新 更多