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];
}

For example In my project:

declare a dictionary

hostToAction = @{
@"redemption": NSStringFromSelector(@selector(openRedeemptionPage:params:)),
@"web": NSStringFromSelector(@selector(openWebViewWithPath:params:absoluteString:))
};

handleOpenURL void

+ (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
{
if ([url.scheme isEqualToString:DJURLSchema] && url.host && hostToAction[url.host]) {
[DJLog info:DJ_UI content:@"handleOpenURL %@ from application %@", url.absoluteString, sourceApplication];

NSString *path = url.path.length > 1 ? [url.path substringFromIndex:1] : @"";
NSDictionary *params = [self dictionaryWithQuery:url.query];
NSString *absoluteString = url.absoluteString;

SEL action = NSSelectorFromString(hostToAction[url.host]);
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:action]];
[inv setSelector:action];
[inv setTarget:self];

[inv setArgument:&(path) atIndex:2];
[inv setArgument:&(params) atIndex:3];
if([url.host isEqualToString:@"web"]) {
[inv setArgument:&(absoluteString) atIndex:4];
}
[inv invoke];
return YES;
}

return NO;
}

openPage void

+ (void)openRedeemptionPage:(NSString *)path params:(NSDictionary *)params
{
dispatch_async(dispatch_get_main_queue(), ^{
[[DJAppCallContainer sharedInstance] appCallRedeemptionPage:path];
});
}

func appCallRedeemptionPage(_ path : String) {
if canShowViewController() {
gotoRedeemptionPage()
}
}

func gotoRedeemptionPage() {
if let vc = DJRedeemptionVC() {
if DJAppCall.isTopVC(vc) {
DJAppCall.getTopvc().navigationController?.popViewController(animated: false)
}
DJAppCall.showViewController(vc)
}
}

func canShowViewController() -> Bool {
let homeMianVC = MainTabViewController.sharedInstance
if let appDelegate = UIApplication.shared.delegate as? AppDelegate{
if appDelegate.window.rootViewController == homeMianVC {
return true
}
}
return false
}

+ (BOOL)isTopVC:(UIViewController *)viewController
{
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UITabBarController *mainViewController = (UITabBarController *)window.rootViewController;

if ([mainViewController.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *nvc = (UINavigationController *)mainViewController.selectedViewController;
if ([nvc.topViewController isKindOfClass:viewController.class])
{
return YES;
}
}
return NO;
}

+ (UIViewController *)getTopvc
{
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UITabBarController *mainViewController = (UITabBarController *)window.rootViewController;

if ([mainViewController.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *nvc = (UINavigationController *)mainViewController.selectedViewController;
return nvc.topViewController;
}
return nil;
}


+ (void)showViewController:(UIViewController *)viewController
{
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UITabBarController *mainViewController = (UITabBarController *)window.rootViewController;
[mainViewController dismissViewControllerAnimated:NO completion:nil];
for (UIViewController *vc in mainViewController.viewControllers) {
[vc dismissViewControllerAnimated:NO completion:nil];
}
if ([mainViewController.selectedViewController isKindOfClass:[UINavigationController class]]) {
viewController.hidesBottomBarWhenPushed = true;
[(UINavigationController *)mainViewController.selectedViewController pushViewController:viewController animated:YES];
}
}

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