Adding a URL scheme to your own app

suggest change

Let’s say you’re working on an app called MyTasks, and you want to allow inbound URLs to create a new task with a title and a body. The URL you’re designing might look something like this:

mytasks://create?title=hello&body=world

(Of course, the text and body parameters are used to populate our task that we’re creating!)

Here are the Big Steps to adding this URL scheme to your project:

  1. Register a URL scheme in your app’s Info.plist file, so the system knows when to route a URL to your app.
  2. Add a function to your UIApplicationDelegate that accepts and handles incoming URLs.
  3. Perform whatever task needs to occur when that URL is opened.

Step One: Register a URL scheme in Info.plist:

First, we need to add a “URL Types” entry to our Info.plist file. Click the (+) button here:

…then enter a unique identifier for your app, as well as the URL scheme you want to use. Be specific! You don’t want the URL scheme to conflict with another app’s implementation. Better to be too-long here than too-short!

Step Two: Handle the URL in the UIApplicationDelegate

We need to implement application:openURL:options: on our UIApplicationDelegate. We’ll inspect the incoming URL and see if there’s an action we can take!

One implementation would be this:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    if url.scheme == "mytasks" && url.host == "create" {
        let title = // get the title out of the URL's query using a method of your choice
        let body = // get the title out of the URL's query using a method of your choice
        self.rootViewController.createTaskWithTitle(title, body: body)
        return true
    }

    return false
}

Step Three: Perform a task depending on the URL.

When a user opens your app via a URL, they probably expected something to happen. Maybe that’s navigating to a piece of content, maybe that’s creating a new item - in this example, we’re going to create a new task in the app!

In the above code, we can see a call to self.rootViewController.createTaskWithTitle(:body:) - so assuming that your AppDelegate has a pointer to its root view controller which implements the function properly, you’re all set!

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Deep Linking:
* Adding a URL scheme to your own app

Table Of Contents
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
98 Deep Linking
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable