Targets:
What is a URL scheme?
How to set a URL scheme?
What can a URL scheme do?
How to apply it in to practice?
How to detect if an app is installed?
How to open another app in an iPhone?
Other operations
1. URL Scheme
URL
As we all know, http://www.apple.com is a URL (Uniform Resource Locator), with which we can locate the address to fetch what we request. Sometimes, we will also call it link or website.
Scheme
A scheme describes a specify location of a URL, the header (the part before ://) . For example http://www.apple.com 's scheme is "http". In application, it is also clear that a URL Scheme can locate another application just like the header of a web's URL. So using URL Scheme we can easily identify the application we need to open or interact with.
2. Set URL Schemes
In xcode, we could identify our own project with a URL Scheme easily.
Step 1: Select your project name at the file root.
Step 2: Find in middle section's menu banner : ->Info->URL Types->“+”->URL Schemes
Step 3: Type your scheme name into the blank
Completed!
We can see the change in the info.plist, there is the URL Scheme (demo) I just added.
3. OpenURL
Now with a url scheme already known, we can use it to open another application installed in your iPhone. Let's start coding:
- (void)openOtherApp {
//create the url we need
NSURL *url = [NSURL URLWithString:@"demo://"];
//open the application with the url
[[UIApplication sharedAppliaction] openURL: url];
}
With the method openURL, we can easily open another application in the iPhone.
This action will just be like the operation that you type >demo:// in safari search bar:
4. CanOpenURL
Usually before open the URL, we should detect if this app is installed or not. So in this way, we need to detect whether the scheme exists or not at first. And this will use the method canOpenURL, which will return the BOOL meaning YES, it can. or NO, it can't. Example coding:
NSString *schemeURL = [[NSString alloc]initWithFormat:@"%@://", scheme];
NSURL *url = [NSURL URLWithString: schemeURL];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[installedApp addObject:scheme];
}
Attention:
However, iOS has the restriction that limits your request for URL Schemes. So if you just run your project calling the methods above, it will crash just like this:
To solve this situation, you are supposed to add some script language into your project' info.plist first.
Open the info.plist as source code, and add following sentences:
LSApplicationQueriesSchemes
micbuye
Finally, you can use this URL Scheme for openURL or canOpenURL at will!
5. Apply
Until now, I believe you definitely have already known how to detect if the application is installed using URL Scheme, and as well using it to open another application. So I just put my codes here for study and communication:
@implementation Detect
//detect a list of apps if they are installed and return the result as a string
+ (NSString *)detectWithArray {
NSArray *appList = @[@"scheme1", @"scheme2", @"scheme3"]; //the list of schemes you want to detect
NSMutableArray *installedApp = [[NSMutableArray alloc] init];
//detect if the scheme exists, if yes->this app is installed
for (NSString *scheme in appList) {
NSString *schemeURL = [[NSString alloc] initWithFormat:@"%@://", scheme];
NSURL *url = [NSURL URLWithString: schemeURL];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[installedApp addObject:scheme];
}
}
//turn this installedapp array into string as the outcome
NSString *installedList = [installedApp componentsJoinedByString:@","];
NSLog(@"Result: %@", installedList);
return installedList;
}
@end
You can also use openURL to implement a method that can open another application if you need.
Thanks for reading.