Created
November 19, 2025 18:07
-
-
Save Tunous/363bb71baa5b401ff8a63304f6389be5 to your computer and use it in GitHub Desktop.
Code to open main app from share extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // A way to open main app from share extension. Verified on iOS 15-26 and macOS 15 | |
| private func openApp() { | |
| // For this to work the app needs to have an url scheme: https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app | |
| // Potential alternative is also to use universal links: https://developer.apple.com/documentation/xcode/allowing-apps-and-websites-to-link-to-your-content | |
| let url = URL(string: "myApp://")! | |
| #if os(iOS) | |
| if #available(iOS 18.0, *), openURL(url) { | |
| return | |
| } | |
| OpenURLAction.system(url) | |
| #else | |
| NSWorkspace.shared.open(url) | |
| #endif | |
| } | |
| #if os(iOS) | |
| @available(iOS 18, *) | |
| private func openURL(_ url: URL) -> Bool { | |
| let responser = sequence(first: self, next: \.next) | |
| .first(where: { $0 is UIApplication }) | |
| if let application = responser as? UIApplication { | |
| application.open(url, options: [:], completionHandler: nil) | |
| return true | |
| } | |
| return false | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment