One of the coolest features of the Iphone/ios SDK is that the app "binds" itself to a custom URL scheme that launches the app from a browser or other app.
registering a custom URL Scheme
The first step in registering a custom URL Scheme is to create a URL scheme-find and click the project info.plist file in Xcode project Navigator. When the file is displayed in the right window, click the right mouse button on the list and select Add Row:
Scroll down the pop-up list and select URL types.
Click on the left-hand cutout to open the list and see Item 0, a dictionary entity. Expand Item 0 To see the URL Identifier, a String object. The string is the name of your custom URL scheme. It is recommended to use reverse domain name method to ensure the uniqueness of the name, such as COM.YOURCOMPANY.YOURAPP.
Click on Item 0 to add a row, select the URL schemes from the drop-down list and hit the keyboard enter to complete the insertion.
Note URL schemes is an array that allows the app to define multiple URL schemes.
Expand the data and click Item 0. You will define the name of the custom URL scheme here. Just need a name, do not append://-For example, if you enter Iosdevapp, your custom URL is iosdevapp://
At this point, the entire definition is as follows:
While I agree with Xcode's purpose of using descriptive names, it is also useful to see the actual key created. Here's a handy tip, right-click on plist and select Show Raw keys/values to see the following effects:
There is another useful output format, XML, because it is very easy to see the structure of the dictionary and the original array and the entities it includes. Click plist and select Open as–source Code:
invoking a custom URL Scheme from Safari
With the URL scheme defined, we can run a quick test to verify that the application is called as we expect. Before that, I created a quasi-UI to identify apps with custom URLs. The app has only one UILabel with the text "App with Custom URL". Download source codes (http://iosdevelopertips.com/downloads/#customURLScheme)
To invoke an app using the emulator:
Run your app in Xcode
Once the application is installed, the custom URL scheme will be registered
Close the app by selecting Home from the emulator's Hardware menu
Start Safari
Enter the URL scheme defined earlier in the browser address bar (below)
Safari will be closed and the app will be brought back to the foreground. Congratulations, you just called an IPhone app using a custom URL scheme.
calling Custom URL Scheme from another IPhone app
Let's see how to invoke the custom URL scheme from another application. I've created a very simple IPhone app that has only one UILabel and one uibutton-that shows a piece of information telling you that the app is going to invoke another app through a custom URL scheme, and the button starts this behavior. Download source codes (http://iosdevelopertips.com/downloads/#customURLScheme)
The code in the Buttonpressed method handles URL calls:
-(void) buttonpressed: (UIButton *) button
{
NSString *customurl = @ "iosdevtips://";
if ([[UIApplication sharedapplication]
Canopenurl:[nsurl Urlwithstring:customurl]])
{
[[UIApplication sharedapplication] Openurl:[nsurl Urlwithstring:customurl]];
}
Else
{
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "URL error"
Message:[nsstring stringWithFormat:
@ "No custom URL defined for%@", CustomURL]
Delegate:self cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alert show];
}
}
The 5th line of code checks to see if the custom URL is defined and, if defined, uses a shared application instance to open the URL (line 8th). OpenURL: Method launches the app and passes the URL to the app. During this process, the current app is exited.
passing parameters to an app through custom URL Scheme
Sometimes you need to pass parameters to your app through a custom URL. Let's see how we can get this job done.
Nsurl is the basis for invoking another from one application, following the RFC 1808 (https://tools.ietf.org/html/rfc1808) (Relative Uniform Resource Locators) standard. So your familiar URL format based on Web content is also available here.
In apps that have customized URL scheme, app delegate must implement the following methods:
-(BOOL) Application: (UIApplication *) application
OpenURL: (nsurl *) URL
Sourceapplication: (NSString *) sourceapplication
Annotation: (ID) annotation
The trick to pass parameters from one app to another is through URLs. For example, suppose we use the following URL scheme, want to pass a parameter named "token" and a flag that identifies the status of registration, we can create a URL like this:
NSString *customurl = @ "Iosdevtips://?token=123abct®istered=1";
In web development, the string? Token=123abct®istered=1 is called a query string.
In app delegate of the app that was called (set a custom URL), get the code for the parameter as follows:
-(BOOL) Application: (UIApplication *) application OpenURL: (nsurl *) URL
Sourceapplication: (NSString *) sourceapplication annotation: (id) annotation
{
NSLog (@ "Calling application Bundle ID:%@", sourceapplication);
NSLog (@ "url scheme:%@", [url scheme]);
NSLog (@ "URL query:%@", [url query]);
return YES;
}
The output of the above code when the application is called is:
Calling application Bundle Id:com.3sixty.callcustomurl
URL scheme:iosdevtips
URL query:token=123abct®istered=1
Note "Calling Application Bundle ID", which you can use to ensure that only the apps you define can interact directly with your app.
Let's change the code to verify that the Bundle ID of the app that originated the call is legitimate:
-(BOOL) Application: (UIApplication *) application OpenURL: (nsurl *) URL
Sourceapplication: (NSString *) sourceapplication annotation: (id) annotation
{
Check the calling application Bundle ID
if ([Sourceapplication isequaltostring:@ "Com.3sixty.callcustomurl"])
{
NSLog (@ "Calling application Bundle ID:%@", sourceapplication);
NSLog (@ "url scheme:%@", [url scheme]);
NSLog (@ "URL query:%@", [url query]);
return YES;
}
Else
return NO;
}
One thing to keep in mind is that you can't prevent other apps from calling your app through a custom URL scheme, but you can skip the next steps and return NO, just like the code above. In other words, if you want to prevent other apps from calling your app, create a different URL scheme. While this does not guarantee that your application will not be invoked, it can at least significantly reduce this possibility.
Custom URL Scheme full guide