Custom URL Schemes

1, the introduction

URL Schemes have been around for a long time on iOS. For users of sandbox iOS, URL Schemes are unavoidable if they want to achieve a certain degree of automation. However, because URL Schemes are not used in the same way as traditional iOS graphical interfaces, it is somewhat difficult for those who are interested in URL Schemes (especially those who are afraid of English) to understand them.

2. Introduce Apple’s sandbox

Apple chose sandbox for privacy and security, but sandbox also prevents proper information sharing between apps, hence URL Schemes.

Generally speaking, the smart devices we use have a lot of our personal information on them. For example: contact information, bank card/credit card information, Alipay /Paypal/ account password of each major mall, photos and even itinerary and location information, etc.

If every app on your device, whether it’s official or one you install from any store, has access to this information at will, you’ll get spam messages and emails at best and the consequences at worst. How to keep this information from being freely used by other applications, or how to use this information only with the knowledge and permission of the device owner, is the core security problem that all smart devices and operating systems care about.

In iOS, Apple addresses this problem by using a mechanism called “sandbox” : an app can only access the resources it claims it can access. All apps submitted to the App Store must comply with this mechanism.

Sandboxes are a good solution in terms of security, but they are overkill. We don’t want to share sensitive personal information, but that doesn’t mean we don’t want to share all that information with other apps.

For example, if we want to put multiple events into a calendar at once (yes, only once), these events contain information such as date, time, and duration, we can’t do this if the apps can’t communicate with each other. (This process is detailed in the X-callback-URL section below.)

Like adding more than one calendar event at a time, we encounter many unnecessary and repetitive steps in the process of using smart devices. Most people are unconscious of these repetitive steps, just as they automatically repeat the renaming process when they have a batch of files on their computer that need to be renamed in bulk. But when we understand how these devices work, or have some tools, we can eliminate all of these repetitive steps. One tool we can use on iOS is URL Schemes.

3. What is URL Schemes

The nice thing about the Custom URL scheme is that you can use this URL to open applications in other applications. Href = ‘myApp://’ Href = ‘myApp://’

Url schemes are better understood by comparing them to web urls. Give a url “bxu2359670321.my3w.com/view/login…. :// hostname[:port] / path / [;parameters][? Query]#fragment So the protocol of this URL is HTTP. By comparing URL Scheme, the example “weixin://dl/moments” is given. The preceding weixin: represents wechat Scheme. You can understand the URL of an iOS app exactly the same way you understand the URL of a web page — that is, its web address. That is, Scheme is the character before ://

Pay attention to

1. All web pages have URLS; But not all apps have their own URL Schemes, and not every app has URL Schemes for every feature

There is only one web site for each URL, but not every URL Schemes has only one app. This is because Apple doesn’t have a hard and fast rule on URL Schemes

3. General web urls are easy to predict, but iOS URL Schemes are difficult to guess because there is no standard. It is not practical to get iOS APP URL Schemes by guessing. (I recommend reversing the Bundle identifier)

On dry

1. Register a custom URL Scheme

1) The first step to register a custom URL Scheme is to create a URL Scheme – find and click on the Project Info.plist file in the Xcode Project Navigator. When the file appears in the right window, right-click on the list and select Add Row:





url scheme1.png

2) Click on the left clipper to open the list. You can see Item 0, a dictionary entity. Expanding Item 0, you see the URL Identifier, a string object. This string is the name of your custom URL scheme. It is recommended to reverse the Bundle idenmtifier to ensure that the name is unique





url scheme2.png

3) Click Item 0 to add a new line, select URL Schemes from the drop down list and press Enter to insert. (Note that URL Schemes are an array, allowing applications to define multiple URL Schemes.) Expand the data and click Item 0. This is where you will define the name of your custom URL scheme. You only need the name, do not append it with ://





url scheme3.png

2, take the browser to sit simple verification

Enter the custom URL scheme in the address bar. In this case, ensure that the App with the user-defined URL scheme is installed on the device where the browser is installed.





IMG_5739.PNG

3. Create a new Xcode project and try to make an App. Here I will put a Button and click to open the URL code.

- (IBAction)open:(id)sender { NSString *url = @"zhunaer://? name=lbp&age=22"; if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; }else{NSLog(@" can't open "); }}Copy the code

And it won’t open. Why? Query Schemes are not included in the plist of the new App

<key>LSApplicationQueriesSchemes</key>
<array>
<string>zhunaer</string>
</array>Copy the code

4. What if you need to transfer values between two Apps? This can be resolved using URL Scheme.

Implemented in the appdelegate. m of the opened App -(BOOL)application:(UIApplication)application openURL:(nonnull NSURL)url SourceApplication :(nullable NSString *)sourceApplication annotation:(nonnull id)annotation;

-(BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation{ NSLog(@"calling application bundle id: %@",sourceApplication); NSLog(@"url shceme:%@",[url scheme]); NSLog(@" parameter :%@",[url query]); if ([sourceApplication isEqualToString:@"com.geek.test1"]) { return YES; } return NO; }Copy the code

Add a parameter to the url of the click event to open the third-party App, like NSString *url = @”zhunaer://? name=lbp&age=22″;

Note: Add? After URL Scheme. Then write it the same way as the url parameter of the web page.

5. How to judge whether to specify the App to open, or some apps do not allow us to open our App? I did the experiment.

A: Change the Bundle identifier to “com.geek.test2” for projects that need to open third-party apps, and keep the rest unchanged

B: In the appdelegate.m of the opened App

-(BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation{ NSLog(@"calling application bundle id: %@",sourceApplication); NSLog(@"url shceme:%@",[url scheme]); NSLog(@" parameter :%@",[url query]); if ([sourceApplication isEqualToString:@"com.geek.test1"]) { return YES; } return NO; }Copy the code




Simulator Screen Shot 2017年5月1日 下午2.48.21.png

The experimental results

You can still open your App even if you break into Return NO

Bottom line: If you want to prevent other apps from calling your app, create a different URL scheme. While this doesn’t guarantee that your application won’t be called, it at least makes it much less likely.

Reference: sspai.com/post/31500#…