IOSer as a mobile developer, I have to say that deep linking has become a compulsory course for us in this era of “traffic”. So what is deep linking? Simply put, you can open the App with a simple “link” and go directly to the content page in the App. The premise is that the App has been installed on the phone, and the App needs to support deep linking. For example, in Safari, I see a news article on the Paper.cn App, “Another New World Heritage in China…” Click the button “Open App” on the scroll Banner below to directly enter the corresponding news page in thepaper.cn News App (installed) :
It can be seen that the use of deep link technology on the mobile terminal greatly eliminates tedious operations such as opening the App, searching for content or clicking somewhere to enter the specified page. Instead, users can directly click the “open” button on the web page to reach the specified page in the App with one click.
The origin of deep links on iOS
Before I go into how deep linking came about, here’s a basic concept that needs to be synchronized with you:
SandBox (SandBox)
SandBox is a mechanism by which iOS forces an app to read only internal data and not other apps’ data.
- On aN iOS device, each App has its own storage space;
- The App can only access the contents of its own sandbox directory, not the contents of other storage space;
- The data request of the application needs to pass the permission detection. If the detection fails, the request is not executed.
Why use sandboxes?
SandBox (SandBox) is a mechanism in the security system, so When Apple designed the iOS system, it took into account the information security between the applications, set the application access limits.
The downside of SandBox
After the sandbox mechanism is used, apps cannot access each other for communication, which makes apps become information islands one by one. (Weak and helpless)
How do YOU solve SandBox problems?
I can’t say what apple’s original intention is, but the problem is obvious. In fact, Apple has realized the problem of App information isolation as early as iOS 4 in 2010, so it launched URL Scheme technology. This technology allows the iOS system to pass parameters to another App through a specific URL. For example, iOSer://userid=123456&name=sands.
It has to be admitted that this technology does solve the thorny problems at the beginning, but in today’s rapidly changing world, THE CRITICISM of URL Scheme is becoming more and more obvious. For example, if you want to realize the jump between two apps, you need to merge and develop, and for another example, the premise of THE URL Scheme to open the App is that the App has been installed. More importantly, more and more browsers no longer support URL schemes, so we have to find another way to do it.
Apple realized that THE URL Scheme was no longer a sustainable solution, so in 2015, apple introduced Universal Links in iOS 9.
Second, deep links to solve the problem
A technique that allows you to launch an App or open a website by clicking on a traditional HTTPS link.
Using a unique url, you can link to a view within a specific App without using a special URI Scheme. For example, if an App shares content to wechat, the user will see the H5 page content in wechat’s built-in browser, and then click the trigger Universal Links link to directly open the same page content in the App. (PS: Because wechat 6.5 version after the shielding operation, it can not directly open the App, but this does not affect the system boot.)
NOTE
Universal links let users open your app when they tap links to your website within WKWebView and UIWebView views and Safari pages, in addition to links that result in a call to openURL:, such as those that occur in Mail, Messages, and other apps.
When a user is browsing your website in Safari and they tap a universal link to a URL in the same domain as the current webpage, IOS Respects the user’s most likely intent and the link in Safari. If the user taps a universal link to a URL in a different domain, iOS opens the link in your app.
For users who are running versions of iOS earlier than 9.0, tapping a universal link to your website opens the link in Safari.
First of all, this is a standard HTTPS link, so why emphasize this? Because it solves a very core problem, even if you do not have the App installed on your device, you will not see the above error “Safari cannot open this page because the url is invalid” when clicking the link. It can be accessed as a normal web page, which provides more possibilities for improving the user experience.
Second, by using Universal Links to jump to your own App to communicate, there is no need to merge and develop between the two apps. There is no need to do any configuration for opening your App in someone else’s App. You only need to develop and configure your own App and web page. The Universal Links implemented between the web page and the App remain in effect regardless of which App your web page is opened in.
Finally, this method has several advantages, such as eliminating the system confirmation prompt box before the URL Scheme jump to the App, which is more direct. In addition, when the App is not installed, click to directly visit the web page. On the one hand, it solves the unknown success or failure of URL Scheme at the browser level. At the same time, it can also present the user with the content of the web page, guide the user to download or manipulate the web page, which is a way to beat the URL Scheme in every way.
3. How to achieve deep links on iOS
Adding support for Universal Links is simple. You can do this in just three steps:
- To create a
apple-app-site-association
Files that contain JSON data about the URLs your App can handle - Upload the one created above
apple-app-site-association
File to your HTTPS enabled Web server. You can place this file in the root directory of your server or.well-known
Subfolder of - Configure and handle Universal Links in your App
Create and upload the Association file
In order to create a secure connection between your website and your App, you establish a trust relationship between them. This trust relationship is established in two steps:
- One is what you add to your website
apple-app-site-association
file - The other one is what you add to your App
com.apple.developer.associated-domains
Permissions (this section is belowPrepare the App to handle Universal LinksIn the introduction)
In your apple-app-site-association file, you can specify the paths in your site that should and should not be treated as generic links. Keep the path list fairly short and match more path sets by using regular wildcards. Here is an example of an apple-app-site-association file that identifies three paths that should be processed as generic links.
{
"applinks": {
"apps": []."details": [{"appID": "9JA89QQLNQ.com.mob.moblink"."paths": [ "/moblink/news/"."/videos/moblink/2019/*"] {},"appID": "ABCD1234.com.mob.moblink"."paths": [ "*"[}]}Copy the code
Pay attention to
Do not add the. Json suffix after the apple-app-site-association file name.
- Apps: The key must exist, and the corresponding value must be an empty array
- Details: The key corresponding to the value is an array of one or more dictionaries, each of which corresponds to an app supported by your website. In addition, the order of the dictionaries in the array determines the order in which the system queries for matches, so you can specify that the app only handles partial paths to your website
- AppID: The key corresponding to the value is the Team ID and then the Bundle ID
- Paths: The key value specifies which paths in your site are arrays of strings that need to be processed (or not) by the app.
If paths do NOT need to be matched, add NOT before the paths, for example, “paths”: [“/moblink/news/”, “NOT /videos/moblink/2010/*”, “/videos/moblink/201?/*”] So you should write the higher priority after the lower priority. If you change the previous example to: “paths”: [“/moblink/news/”, “/videos/moblink/201?/*”, “NOT /videos/moblink/2010/*”] [“/moblink/news/”, “/videos/moblink/201?/*”, “videos/moblink/2010/*”]
There are several ways to specify the site path in the apple-app-site-association file. You can:
- with
*
To specify that your entire page is supported - Contains a special URL, such as
/moblink/news
To specify partial link support - After the special URL
*
, e.g./videos/moblink/2017/*
To specify a set of link support
Note that the paths in the Paths array are case sensitive and must be case sensitive.
After creating the apple-app-site-association file, upload it to the root directory or. Well-known subdirectory of the HTTPS Web server. The file needs to be accessed directly via HTTPS – no redirection required – at https://
/apple-app-site-association or https:// < domain > /. Well – known/apple – app – site – association. Next, you need to handle Universal Links in your App.
Prepare the App to handle Universal Links
Universal Links uses two techniques: the first is to enable the same mechanism of Handoff between the Web browser and the App, and the second is to share Web credentials. For details about the two technologies, see the official Web Browser-to-Native App Handoff and Shared Web Credentials Reference. When the user clicks on a Universal Link, iOS launches your App and brings an NSUserActivity object parameter to your App that allows you to query how your App was launched.
To support Universal Links in your App, just do the following two steps:
- Add a specified domain name that your App supports
- Update your
AppDelegate
When receiving theNSUserActivity
Object
In your App com. Apple. Developer. Yahoo – domains permissions, contains all the App you want to as a universal link processing domain name list. On the Xcode project home page, select the Capabilities TAB, then open Associated Domains with applinks: Start by adding a domain that your App supports, such as applinks:z.t4m. Cn, applinks:*.ulml.mob.com
Note: This domain rights list can add up to 20 to 30 names.
With Associated Domains configured, implement HandOff’s UIApplicationDelegate method in AppDelegate.m so that your App can receive the Universal Link and handle it appropriately.
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
NSLog(@"title: %@", userActivity.title);
// NSUserActivityTypeBrowsingWeb
NSLog(@"activityType: %@", userActivity.activityType);
// The webpage URL property always contains an HTTP or HTTPS URL, and you can use NSURLComponents APIs to manipulate the components of the URL.
NSLog(@"webpageURL: %@", userActivity.webpageURL); // <your code here... >return YES;
}
Copy the code
At this point, the implementation of deep linking on iOS is complete. Deep links on the whole, realize the iOS involved before and after the operation and alignment will be much more special, especially the HTTPS certificate and error handling access to web sites that piece, looks be like simple, but really do it step by step, big and small problem or a lot, if for front and back side is not particularly familiar with may still need some time. Here I suggest you choose third-party deep link services, such as MobLink, Magic Window, LinkedMe, etc. In contrast, MobLink’s world-leading mobile scene restoration solution is famous for its free, good service and functions, so it is recommended.
WWDC2019: What’s New in Universal Links
After many years, WWDC 2019 just ended with the mention of Universal Links again. The session was short, less than 20 minutes long, but it was long enough to experience apple doing it all the time.
First, a quick note: Universal Links, which was previously only available on iOS and tvOS, is now fully available on macOS, whether you’re developing a Mac App using UIKit or AppKit.
There are two major changes to WWDC2019 Universal Links this time around:
- The apple-app-site-association file supports refined address matching
- Full support for macOS
The OverView OverView
The basic mechanism of Universal Link
- By configuring the relevant information and security domain name in the XCode App configuration
- Deploy a configuration apple-app-site-association file at the secure domain name Https only
- The apple-app-site-association file contains rich links to websites and apps
- A secure and effective handshake mechanism has been established between website and APP
- Realize direct linkage between website URL and app
- Universal Link gives you a much safer and smoother experience than custom schemes
Supplement: schemes on the most annoying is app hijacked, many app will pretend to be registered with well-known companies app scheme to intercept the conventional scheme evoke a consortium, this point in the Universal Link can avoid very well.
Apple-app-site-association File refined matching
Above is an old example of how to write the Apple-app-site-association file for MobLink, an SDK that provides deep linking.
This is a new way of writing and supports more and stronger configuration capabilities.
- The apps field is only useful on iOS. The tvOS/macOS field can be ignored
- The details field structure has changed dramatically
- Previously, the dictionary appID was a Key, but now it is an array, and supports keys such as appIDs, which can be configured with multiple appIDs, greatly reducing the workload
- The Components field is added to further restrict the validity of the Universal Link
- The supported PATH format conditions can be configured with /
- The supported anchor conditions can be configured using #
- Can I pass? To configure supported field conditions
- Exclude is an excluded field. A Universal Link that meets this condition does not take effect
Configuration recommendations for the Universal Links network service
Apple recommends following a few Tips when deploying web services and configuring the Apple-app-site-Association file
- HTTPS must be supported:
The certificate must be a formal one, and cannot be customized.
- The URL uses ASCII encoding
Because you use a lot of symbols in urls like #? %, so use ASCII encoding.
- Reduce the size of the apple-app-site-association file
Reducing the size of the Universal Link helps to work more smoothly under congested network conditions. If your application is country-specific and has different profiles for each country, you can configure them separately using country markers to allow users to download only the minimal JSON files.
- Download and update policy for apple-app-site-Association file:
When the App is installed on the device, it downloads the JSON file from the configuration domain name to the local device so that related functions can take effect according to the configuration. In addition, the JSON file downloaded on the device will be updated irregularly (according to the personal test, the UPDATE of the App release version will also accurately trigger the UPDATE of the JSON file).
- Using the Smart Banner
The Smart Banner and Universal Links on the web page can be used together to effectively guide users who have not installed the APP to download, and users who have installed the APP can smoothly jump to the download
Universal Links supports macOS
Configure Universal Links in XCode
Development of Universal Links code for macOS:
// Configuring Your App
func application(_ application: Application, continue userActivity: NSUserActivity,
restorationHandler: @escaping ([ UserActivityRestoring]?) -> Void) -> Bool {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL,
let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
return false
}
for queryItem incomponents.queryItems ?? [] {...return true
}
Copy the code
In the case of macOS support, the appDelegate’s Universal Link is also the delegate, except that UIApplication is changed to NSApplication.
Due to the delegate will be triggered by a lot of other shortcuts, triggered by the Universal Link not just, so judging by NSUserActivityTypeBrowsingWeb source, the rest is the criteria of the url.
Universal Links on macOS works a little differently than it does on iOS
- The web page is opened by default, and the user will be prompted whether to open the app
- The remote login does not take effect
- The App Store signature macApp is available immediately after the App is downloaded
- The developer signed macApp must take effect after the user has run it once
Open a Universal Link in the MAC App
// UIApplication
UIApplication.shared.open(url, options: [ .universalLinksOnly: true]) {... } // NSWorkspacelet configuration = NSWorkspace.OpenConfiguration()
configuration.requiresUniversalLinks = trueNsworkplace.shared. open(url, configuration: configuration) {... }Copy the code