1

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?

timgcarlson
  • 3,017
  • 25
  • 52
  • As far as I know, there's still no way to do this from the share extension. Best you could do is create a "save to {yourapp}" and have it upload the file into your app, ready to go as a draft. User would still manually have to open the app themselves though. – brandonscript Jan 03 '17 at 18:06
  • I was afraid that'd be the answer I'd get. Hoping there is some way to do this, and if not hoping it will be added in a later version of iOS. – timgcarlson Jan 03 '17 at 19:11
  • 1
    File a radar? Probably the only way to get something to happen. – brandonscript Jan 03 '17 at 19:13
  • Duplicate of http://stackoverflow.com/questions/24895093/openurl-doesnt-work-in-share-extension. Short answer, there are some ways to do this. – Jake Jan 04 '17 at 00:01

1 Answers1

7

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
    }
}
Community
  • 1
  • 1
timgcarlson
  • 3,017
  • 25
  • 52
  • Sorry but this is not working for me with custom share UI in swift 3 – Kautham Krishna Sep 20 '17 at 05:34
  • @KauthamMurugan It works for me in Swift 3. Are you using replacing the url string with the one for your app? – timgcarlson Sep 20 '17 at 17:07
  • Yes i am using my own app url. But in my app, i need to show a custom share UI with the user input elements and progress bar.So i have replaced the `SLComposeServiceViewController` to `UIViewController` to have custom designed share controller. I think this should be the problem but i am not sure of it. – Kautham Krishna Sep 22 '17 at 15:50
  • This does not work with the new iOS11 screenshot tool as there is no UIResponder!? – coyer Nov 08 '17 at 09:37
  • @coyer I haven't tried using this on iOS11, but how does the screenshot tool factor into it? This is to open the app from a share extension. – timgcarlson Nov 08 '17 at 22:09
  • @timgcarlson I was wrong, it will work with the new screenshot tool. My app crashed because the iOS 11 screenshot editor returns a UIImage, while apps like Photos would return URL's. I still found no way to debug my shareextension when started within the screenshot-tool. – coyer Nov 10 '17 at 10:07