Search code examples
iosswiftios-messages-extension

iOS messages extension how to switch host app directly


I have an app and I wanna add messages extension feature.

I thought the feature is if the user selects a message, it switches my host app directly like google map.

I made a MSMessage and set URL and the message has template layout which had caption and sub-caption.

let message = MSMessage()
message.url = "http://blahblah?customScheme=myHostAppLaunchScheme"
let template = MSMessageTemplateLayout()
template.image = sampleImage
template.caption = "this is a caption"
template.subCaption = "this is a sub caption"
message.layout = template

guard let conversation = activeConversation else {
      print("blahblah")
      return
}

conversation.insert(message) { (error) in
      print("finish. error = \(error == nil ? "nil"  : error!.localizedDescription)")
}

and i wrote a code extensionContext.open(url, completionHandler) in

willBecomeActive(with conversation: MSConversation)
didReceive(_ message: MSMessage, conversation: MSConversation)

of course, i parsed selectedMessage's URL.

but it didn't work I expected.

the messages extension switches expand mode automatically.

it works if I used

conversation.insertText("myHostAppLaunchScheme", nil)

but I don't want it because it can't add template :(

is there any idea to switch iMessage to host app directly?

thanks for any ideas.


Solution

  • i think i found the answer.

    there is no way with using

    conversation.insert(message, completionHandler)
    

    i think apple music and google maps are using

    conversation.insertText("some url", completionHandler)
    

    because i copied an URL after long press a message which is shared by apple music or google map

    then i use the URL in my code

    conversation.insertText("the URL", completionHandler)
    

    it's working they did!!