2

I'm trying to open my mother app from message extension on a click of button. I've used this code in my exstension:

    @IBAction func open(_ sender: UIButton) {

    let url = URL(string: "swiftexamples://")

    self.extensionContext?.open(url!, completionHandler: {(succes) in })


}

Everything works fine when my mother app is running and backgrounded, however when i want to open closed app, it crashes. There are no crash logs, i have simmilar situation to this iOS app crashes when first opened by URL Scheme . I'm quite sure i have to add something to app delegate. I've found function application(_:open:options:). The problem is, i don't now how to implement "option" part. I've written this:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
     let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? NSURL 
       let sourceApp = launchOptions?[UIApplicationLaunchOptionsKey.sourceApplication] as? String
       let annotation = launchOptions?[UIApplicationLaunchOptionsKey.annotation] as? AnyObject

        self.application(application, open: url, options:[sourceApp: String, annotation:AnyObject] )



    return true
}

Also i've heard about Universal Links and that they have similar abilities to url schemes. Is it possible to do achive what i want using universal links ?

Community
  • 1
  • 1

1 Answers1

0

You can try this solution. It worked for me:

func openUrl(url: URL?) {
    let selector = sel_registerName("openURL:")
    var responder = self as UIResponder?
    while let r = responder, !r.responds(to: selector) {
        responder = r.next
    }
    _ = responder?.perform(selector, with: url)
}

func canOpenUrl(url: URL?) -> Bool {
    let selector = sel_registerName("canOpenURL:")
    var responder = self as UIResponder?
    while let r = responder, !r.responds(to: selector) {
        responder = r.next
    }
    return (responder!.perform(selector, with: url) != nil)
}

As proposed in https://stackoverflow.com/a/44694703/2064473

Martin
  • 1,112
  • 1
  • 11
  • 31