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:
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!
Written by Christopher Hannah
Related protips
5 Responses
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?
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.
Looks like handleOpenURL has been replaced by openURL https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622964-application?language=objc
Any Idea how to get the string into Unity...That would be a great help
Great tip!