Let’s define something in this scenario :
1.Receiver app – to be invoked via custom URL
2.Sender app – who wants to invoke the app
3.Custom URL scheme – define as “happygo”
For Receiver app, here’s what to do:
1.Define the url scheme in Project/Targets/Info/URL Types as follow
2.AppDelegate.m – Two methods to be implemented.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES; //must return YES
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
//Check if the scheme was valid
if ([[url scheme] isEqualToString:@"happygo"]) {
NSString *query = [url query];
NSArray *queryComponents = [query componentsSeparatedByString:@"&"];
for (int i=0; i<[queryComponents count]; i++) {
NSArray *oneParam = [[queryComponents objectAtIndex:i] componentsSeparatedByString:@"="];
NSString *sValue = @"";
NSString *sKey = [oneParam objectAtIndex:0];
if (oneParam.count==2) {
sValue = [oneParam objectAtIndex:1];
}
}
//Here is what you want to do, write here.
return TRUE; //return TRUE because of valid scheme
}
return FALSE; //invalid scheme, skip process
}
For Sender app, here’s what to do :
1.Add the following to info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>happygo</string>
</array>
2.Invoke the receiver app via URL form, sample source code as follow
NSString *sURL = [NSString stringWithFormat:@"happygo://www.domain.com?name=Good&phone=123123"];
NSURL *myURL = [NSURL URLWithString:sURL];
if ([[UIApplication sharedApplication] canOpenURL:myURL]) {
[[UIApplication sharedApplication] openURL:myURL];
} else {
NSLog(@"--bad url--");
}
Actually, you can even invoke the app via Safari Browser.
Just input the URL in Safari, press GO, and the receiver app will be invoked.