royherma
royherma

Reputation: 4193

Redirect back to app from Share Extension

Is it possible to redirect a user back to my app from my share extension (after finishing the posting/other action)?

I couldn't get UIApplication.sharedApplication() to work - method was unavailable.

Any ideas if this is possible/if Apple even allows it?

Upvotes: 1

Views: 3000

Answers (3)

Ashish Yadav
Ashish Yadav

Reputation: 11

I have tested this solution in iOS 18.

@objc @discardableResult private func openURL(_ url: URL) -> Bool {
    var responder: UIResponder? = self
    while responder != nil {
        if let application = responder as? UIApplication {
            if #available(iOS 18.0, *) {
                application.open(url, options: [:], completionHandler: nil)
                return true
            } else {
                return application.perform(#selector(openURL(_:)), with: url) != nil
            }
        }
        responder = responder?.next
    }
    return false
}

Upvotes: 1

Bogdan Farca
Bogdan Farca

Reputation: 4106

Actually this is possible by searching for UIApplication up in the responder chain and invoking openURL on it, like described in this response : https://stackoverflow.com/a/28037297/554203.

This is the code that works for XCode 8, Swift 3.0 and iOS10 (again, extracted from Julio Bailon's response above):

let url = NSURL(string:urlString)
let context = NSExtensionContext()
context.open(url! as URL, completionHandler: nil)

var responder = self as UIResponder?

while (responder != nil){
    if responder?.responds(to: Selector("openURL:")) == true{
        responder?.perform(Selector("openURL:"), with: url)
    }
    responder = responder!.next
}

Upvotes: 2

royherma
royherma

Reputation: 4193

As of iOS 9.3, this does not seem to be possible for a Share Extension.

Upvotes: 0

Related Questions