mtjaeq
Last Updated: October 18, 2018
·
51.43K
· chrishannah

iOS Custom URL Scheme

I'm making an app using a custom URL scheme, so I thought I'd share it with everyone else incase they were curious.

Step 1
Go into your app's info.plst file.

Step 2
Add a Row to this and call it "URL types"

Step 3
Expand the first item in "URL types" and add a row called "URL identifier", the value of this string should be the reverse domain for your app e.g. "com.yourcompany.myapp".

Step 4
Again, add a row into the first item in "URL types" and call it "URL Schemes"

Step 5
Inside "URL Schemes" you can use each item as a different url you wish to use, so if you wanted to use "myapp://" you would create an item called "myapp".

After all of this, your structure should look something like this:
Picture

Using the URL Scheme
Now you've registered the URL with the app, you can start the application by opening a url with the custom scheme.

Here are a few examples:

myapp://
myapp://a/random/path
myapp://?foo=1&bar=2

This url will send a message to the UIApplicationDelegate so if you want to provide a custom handler for it, all you need to do is provide an implementation for it in your delegate.

Something like this:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  // Do something with the url here
}

Most people will want to parse the URL and store it in the NSUserDefaults, here is a example of how you could do that:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  if (!url) { 
    return NO; 
  } 
  NSString *URLString = [url absoluteString]; 
}

Thats it!
I hope this may be useful for you!

4 Responses
Add your response

1626

Hi. Great tip. But I'm not sure why one would want to store the url in the user defaults. Usually you'd want to parse it and perform the corresponding action... Any thoughts?

over 1 year ago ·
1628

It's mainly to store for later use, but your right there's not much use for NSUserDefaults as you could easily store it in an NSURL.

over 1 year ago ·
30564

Here on our website https://windowsclassroom.com/best-ways-remove-write-protection-pen-drive you will get the most easiest and best ways to remove write protection pen drive in easiest way with the help of our tutorial.

about 2 months ago ·
31015