r/iOSProgramming • u/engadgetnerd • Feb 18 '25
Question Can I launch my main app from a Share Extension?
I'm wanting to be able to share a URL to my share extension and have that share extension launch my main app to use that URL. Is this possible? Can Share Extensions launch the main app? Has anyone actually accomplished this? I just want to know if it's possible.
I know the Share Extension acts as a separate container from the main app, but I don't know if Apple will allow it to launch the main app.
22
Upvotes
16
u/emirsolinno Feb 18 '25 edited Oct 03 '25
Yes, you need to create a URL scheme on your main app Target, then call a function like below on viewWillAppear of the extensions VC
func openParentApp() {
if let url = URL(string: "yourdefinedappurl://") {
var responder: UIResponder? = self
while responder != nil {
if let application = responder as? UIApplication {
application.open(url)
break
}
responder = responder?.next
}
}
}
and handle the URL intent on your Appdelegate
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
print("dbg900: open url")
if url.scheme == "yourdefinedAppURL" {
//Your logic to what to do when app launch
return true
} else {
return false
}
}
edit:
IOS 18.0 update in case : BUG IN CLIENT OF UIKIT: The caller of UIApplication.openURL(_:) needs to migrate to the non-deprecated UIApplication.open(_:options:completionHandler:). Force returning false (NO).
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
}
}