IOS-custom URL Schemes for inter-Application Communication

Source: Internet
Author: User

Inter-Application Communication: 1. Learn about URL Schemes. URL Scheme is something like http: //, ftp: //. You can also customize URL Scheme for your own applications, other applications can access your application by using this identifier. If the custom URL Scheme is the same as that of the system application, the system application is called instead of the custom application. Example: invoking: // com. hello/yourpath /? Username = WT & password = 123456 & callback = myapp where invoking is URL Scheme, that is, [url scheme], com. hello is host, that is, [url host], yourpath is path, that is, [url path], username = WT & password = 123456 & callback = myapp is query, that is, [url query]. 2. Call a self-developed application 1) register a custom external interface in the plist File

CFBundleURLName (URL Identifier) A string containing the abstract name of the URL scheme. to ensure uniqueness, it is recommended that you specify a reverse-DNS style of identifier, for example, com. acme. myscheme. the string you specify is also used as a key in your app's InfoPlist. strings file. the value of the key is the human-readable scheme name.

CFBundleURLSchemes (URL Schemes) An array of strings containing the URL scheme names-for example, http, mailto, tel, and sms.

You can set multiple URLs (Schemes) as follows:


The caller's code is as follows:

-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. CGRect rectTextView = CGRectMake (10.0f, 30366f, 300366f, 100366f); self. textView = [[UITextView alloc] initWithFrame: rectTextView]; [self. textView. layer setBorderColor: [UIColor lightGrayColor]. CGColor]; [self. textView. layer setBorderWidth: 0.5f]; [self. textView setText: @ "username = WT & password = 123456 & callback = invok Ing "]; [self. view addSubview: self. textView]; CGRect rect = CGRectMake (10.0f, 1500000f, 3000000f, 400000f); UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; button. frame = rect; [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; [button setTitle: @ "Login" forState: UIControlStateNormal]; [button setBackgroundColor: [UIColor blueColor]; [button addTarget: self act Ion: @ selector (handle :) forControlEvents: UIControlEventTouchUpInside]; [button. layer setMasksToBounds: YES]; [button. layer setCornerRadius: 5.0f]; [self. view addSubview: button];}-(void) handle :( id) sender {NSURL * url = [NSURL URLWithString: [NSString stringWithFormat: @ "invoked: // com. hello/path? % @ ", Self. textView. text]; if ([[UIApplication sharedApplication] canOpenURL: url]) {[[UIApplication sharedApplication] openURL: url];} else {UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "message" message: [NSString stringWithFormat: @ "% @", url] delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alertView show];}

Receiving code of the caller:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{    NSLog(@"%@", url);    if ([[url scheme] isEqualToString:@"invoked"]) {        if ([[url host] isEqualToString:@"com.hello"]) {            NSString *query = [url query];            NSArray *array = [query componentsSeparatedByString:@"&"];                        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10];            for (NSString *item in array) {                NSArray *valueArray = [item componentsSeparatedByString:@"="];                [dic setValue:[valueArray objectAtIndex:1] forKey:[valueArray objectAtIndex:0]];            }            [self application:application didFinishLaunchingWithOptions:dic];        }        return YES;    }    return NO;}

Similarly, you can set some reserved fields in the input parameters for future extension. To implement callback, you can add a callback field, for example: callback = follow the URL Scheme of your application after myapp, and then return the execution result. Code processing is the same as that of the called party.
Click to download the complete code

References:

Https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html# // Apple_ref/doc/uid/TP40007072-CH7-SW20

Http://blog.csdn.net/likendsl/article/details/7553605

Http://www.cocoachina.com/newbie/tutorial/2012/0529/4302.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.