Pass parameters to the target app via the registered URL scheme

Source: Internet
Author: User

Pass parameters to the target app via the registered URL scheme

Launching the app via URL scheme is simple enough, but sometimes we want to pass some parameters when we launch the app, so we can pass the parameters by URL scheme custom URL.

Yesterday we called the Proxy method of uiapplicationdelegate in Appdelegate:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL*)url{    // 接受传过来的参数    NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开啦"                                           message:text                                          delegate:nil                                 cancelButtonTitle:@"OK"                                 otherButtonTitles:nil];    [alertView show];    return YES;}

Let's take a look at the notes Apple gives to this method:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;  // Will be deprecated at some point, please replace with application:openURL:sourceApplication:annotation:

This method will be discarded in the future and can be replaced by application:openURL:sourceApplication:annotation:

URL parameter format

Yesterday we registered in the Iosstrongdemo URL scheme still remember what is it? You should also be impressed with the iOS development ID:iosdevtip.

Let's say we want to pass two parameters, name and phone number, respectively, in the following format:

iOSDevTip://?name=ligang&phone=13888888888

There is no sense of déjà vu. We ask for an interface in get mode.

The app that was launched handles the passed parameters.
- (BOOL)application:(UIApplication *)application        openURL:(NSURL *)url  sourceApplication:(NSString *)sourceApplication         annotation:(id)annotation{    NSLog(@"sourceApplication: %@", sourceApplication);    NSLog(@"URL scheme:%@", [url scheme]);    NSLog(@"URL query: %@", [url query]);    // 接受传过来的参数    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开啦"                                                        message:[url query]                                                       delegate:nil                                              cancelButtonTitle:@"OK"                                              otherButtonTitles:nil];    [alertView show];    return YES;}

When the app is started, the proxy method application:openURL:sourceApplication:annotation is called:. The parameter URL is the URL that launches the app, and the parameter sourceapplication is the bundle ID of the source app.

We still test through safari and type in Safari's Address bar: iosdevtip://?name=ligang&phone=13888888888

You can open the app and see if the parameters pass through:

Finally, let's look at the print:

2015-07-15 22:38:25.655 iOSStrongDemo[9983:2894855] sourceApplication: com.apple.mobilesafari2015-07-15 22:38:28.664 iOSStrongDemo[9983:2894855] URL scheme:iosdevtip2015-07-15 22:38:28.665 iOSStrongDemo[9983:2894855] URL query: name=ligang&phone=13888888888

Sourceapplication print Out is Com.apple.mobilesafari, from here can be seen from Safari to launch our app.

Although we have customized URL scheme, we cannot prevent others from opening our app with a custom URL scheme. How to solve it?

We can specify the corresponding sourceapplication, that is, the bundle ID, through the bundle ID to decide whether we can open our app:

- (BOOL)application:(UIApplication *)application        openURL:(NSURL *)url  sourceApplication:(NSString *)sourceApplication         annotation:(id)annotation{    NSLog(@"sourceApplication: %@", sourceApplication);    NSLog(@"URL scheme:%@", [url scheme]);    NSLog(@"URL query: %@", [url query]);    if ([sourceApplication isEqualToString:@"com.3Sixty.CallCustomURL"]){        // 接受传过来的参数        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开啦"                                                            message:[url query]                                                           delegate:nil                                                  cancelButtonTitle:@"OK"                                                  otherButtonTitles:nil];        [alertView show];        return YES;    }else{        return NO;    }}

This allows us to decide whether to allow our App,demo:iosstrongdemo to be opened by the bundle ID.

The source of this article just online: http://www.superqq.com/blog/2015/07/15/tong-guo-url-schemechuan-di-can-shu/

Pass parameters to the target app via the registered URL scheme

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.