iosswiftsafariimessage-extension

ios 11 imessage extension message.url does not open safari


I'm adding an iMessage extension target to my app. The extension is supposed to send a message that has a url attribute. The behaviour I'm expecting when a user touches the message is to open the browser using the url attribute of the message.

I have a button in my messageView which executes this code:

@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)
    }

If I touch the message, it expands the MessageViewController

I have then added this:

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)
        }
}

And now, when I touch the message, it opens my main app but still not my browser.

I have seen on another post (where I cannot comment, thus I opened this post) that it is impossible to open in Safari but I have a news app which inserts links to articles and allows with a click on the message to open the article in a browser window, while the app is installed.

So, can someone please tell how I can proceed to force opening the link in a browser window?

Thank you very much.


Solution

  • Here is a trick to insert a link in a message. It does not allow to create an object that has an url attribute but just to insert a link directly which will open in the default web browser.

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