Swift version: 5.10
Custom URL schemes allow your app to be launched from anywhere else in the system, but you can also use them to query which of your other apps are installed and even pass data.
To register your custom URL scheme, go to your project settings, select your target, then go to the Info tab. Underneath the rows from your Info.plist file are Document Types, Exported UTIs, Imported UTIs, and URL Types – you need to open that last disclosure indicator.
There are a selection of properties you can add for each URL type you add, but really you only need two: an identifier that is unique to your app and that URL, plus the URL scheme that should be used. For identifier enter “com.yourcompany.yourapp.yoururl”, e.g. com.apple.pages.open, and for URL schemes enter just the part you want before “://“, e.g. you should enter “myapp” if you want to use “myapp://“.
That’s enough to make your app launch when that URL is triggered, so now you just need to respond to a URL. iOS will call a particular method on your app delegate whenever a URL is passed in by the system, so you’ll probably want to send that on to a view controller of your choosing.
Here’s some code to print out the URL:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print(url)
return true
}
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 3.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.