简体   繁体   中英

Open the main app from a iOS Safari Share Extension

I have a Safari share extension where I want the ability to open the main app from within the extension. The user is presented with an alert where they have the option to open the app.

func openAppHandler() {
    self.extensionContext?.completeRequest(returningItems: []) { (success) in
        if let url = URL(string: "myapp://...") {
            self.extensionContext?.open(url, completionHandler: nil)
        }
    }
}

The alert appears after the method didSelectPost() is called, and as you can see it occurs in the background priority completion block for the extension. The open method says in it's docs "In iOS 8, only the Today extension point (used for creating widgets) supports this method." I'm guessing it's still the case that it's still not supported in the Safari Share Extension.

Does anyone know of a way to open my main app from a share extension?

I found a solution here . I'm not sure if this is technically ok with Apple, but it works just as I need it to.

@objc func openURL(_ url: URL) {
    return
}

func openContainerApp() {
    var responder: UIResponder? = self as UIResponder
    let selector = #selector(MyViewController.openURL(_:))

    while responder != nil {
        if responder!.responds(to: selector) && responder != self {
            responder!.perform(selector, with: URL(string: "myapp://url")!)
            return
        }
        responder = responder?.next
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM