NEW: Nominations are now open for the 2019 Swift Community Awards! >>

How to make your app open with a custom URL scheme

Swift version: 5.1

Paul Hudson    @twostraws   

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
}

SAVE 20% ON iOS CONF SG The largest iOS conference in Southeast Asia is back in Singapore for the 5th time in January 2020, now with two days of workshops plus two days of talks on SwiftUI, Combine, GraphQL, and more! Save a massive 20% on your tickets by clicking on this link.

Available from iOS 3.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 2.3/5