Swift 3 iMessage Extension doesn't open URL

為{幸葍}努か 提交于 2019-12-04 10:30:43

I think safari Browser only opens for macOS. This worked for me:

override func didSelectMessage(message: MSMessage, conversation: MSConversation) {

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

            // Eg. open your app:
            let url = // your apps url
            self.extensionContext?.openURL(url, completionHandler: { (success: Bool) in

            })
        }
    }

Using the technique shown by Julio Bailon

Fixed for Swift 4 and that openURL has been deprecated.

Note that the extensionContext?.openURL technique does not work from an iMessage extension - it only opens your current app.

I have posted a full sample app showing the technique on GitHub with the relevant snippet here:

    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
    }

Here is the code I use to open a URL from a iMessage extension. It is currently working to open the Music app in the WATUU iMessage application. For instance with the URL "https://itunes.apple.com/us/album/as%C3%AD/1154300311?i=1154300401&uo=4&app=music"

This functionality currently works in iOS 10, 11 and 12

func openInMessagingURL(urlString: String){
    if let url = NSURL(string:urlString){
        let context = NSExtensionContext()
        context.open(url, completionHandler: nil)
        var responder = self as UIResponder?

        while (responder != nil){
            if responder?.responds(to: Selector("openURL:")) == true{
                responder?.perform(Selector("openURL:"), with: url)
            }
            responder = responder!.next
        }
    }
}

UPDATE FOR SWIFT 4

func openInMessagingURL(urlString: String){
    if let url = URL(string:urlString){
        let context = NSExtensionContext()
        context.open(url, completionHandler: nil)
        var responder = self as UIResponder?

        while (responder != nil){
            if responder?.responds(to: #selector(UIApplication.open(_:options:completionHandler:))) == true{
                responder?.perform(#selector(UIApplication.open(_:options:completionHandler:)), with: url)
            }
            responder = responder!.next
        }
    }
}

It seems it is not possible to open an app from a Message Extension, except the companion app contained in the Workspace. We have tried to open Safari from our Message Extension, it did not work, this limitation seems by design.

You could try other scenari to solve your problem :

  1. Webview in Expanded Message Extension

    You could have a Webview in your Message Extension, and when you click on a message, you could open the Expanded mode and open you Url in the Webview.

The user won't be in Safari, but the page will be embedded in your Message Extension.

  1. Open the Url in the Companion App

    On a click on the message, you could open your Companion app (through the Url Scheme with MyApp://?myParam=myValue) with a special parameter ; the Companion app should react to this parameter and could redirect to Safari through OpenUrl.

In this case, you'll several redirects before the WebPage, but it should allow to go back to the conversation.

We have also found that we could instance a SKStoreProductViewController in a Message Extension, if you want to open the Apple Store right in Messages and let the user buy items.

If you only need to insert a link, then you should use activeConversation.insertText and insert the link. Touching the message will open Safari.

  1. openURL in didSelectMessage:conversation: by using extensionContext

  2. handle the URL scheme in your host AppDelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!