通用鏈接(Universal Links)就不詳細介紹了. 官方文檔已經(jīng)十分詳細: Universal Links 蘋果官網(wǎng)文檔
Universal Links是iOS9推出的一項功能惠勒,使你的應(yīng)用可以通過傳統(tǒng)的HTTP鏈接來啟動APP或者打開網(wǎng)頁。
一.前期準(zhǔn)備工作
TeamID:XXXXXX (即團隊ID, 在蘋果開發(fā)者網(wǎng)站 Apple Developer -> Member Center ->Membership->Team ID)
Bundle Identifier:xx.xxx.xx
需要生成一個json文件, 名為apple-app-site-association, 注意: 不需要添加任何后綴 !
內(nèi)容如下:
{
"applinks": {
"apps": [],
"details": [
{
"appID": "XXXXXX.xx.xxx.xx",
"paths": [ "*" ]
}
]
}
}
注:
appID: 內(nèi)容為 TeamID+Bundle Identifier , 例 XXXXXX.xx.xxx.xx
paths:設(shè)定你的app支持的路徑列表,只有這些指定的路徑的鏈接竞帽,才能被app所處理。星號的寫法代表了可識別域名下所有鏈接晕拆。
有多種方法可以在apple-app-site-association文件中指定網(wǎng)站路徑淤堵。例如,您可以:
使用 * 指定整個網(wǎng)站
1.包括特定的URL稀拐,例如/sub/im/
,以指定特定的鏈接
2.附加 * 到特定網(wǎng)址丹弱,例如/sub/video/*
德撬,指定您網(wǎng)站的某個部分
3.指定不應(yīng)作為通用鏈接處理的區(qū)域铲咨,請在路徑字符串的開頭添加“NOT”(包括后面的空格)
例如
"paths": [ "/sub/im/", "/sub/video/*", "NOT /sub/nor/2021/*"]
二.服務(wù)端
這里需要后臺的小伙伴配合一下
1.服務(wù)端提供一個支持https的域名(必須支持https)
2.將上面生成好的json文件(apple-app-site-association)放到上面域名的服務(wù)器根目錄下或者.well-known目錄下
三.iOS客戶端
配置開發(fā)者平臺
1.登陸蘋果開發(fā)者管理后臺
2.打開 Member Center --- Certificates, Identifiers & Profiles
3.打開 Identifiers,找到項目對應(yīng)的Identifier并打開
4.勾選Associated Domains選項
項目配置
5.打開我們的項目工程蜓洪,然后繼續(xù)打開我們的工程配置中的Signing&Capabilities
6.點擊左上角的“+Capability”纤勒,然后選擇Associated Domains
7.輸入對應(yīng)的Domains 例:你的Universal Links域名為www.test.com
, 那么你的Domains的內(nèi)容即為: applinks:test.com
- 最后一步, 在appDelegate中, 添加如下的代碼
@interface AppDelegate () {
//1.解決打開未啟動的app無法跳轉(zhuǎn)指定頁面的問題
dispatch_group_t dispatchGroup;
}
@end
@implementation AppDelegate
//當(dāng)用Universal Links啟動APP時就會調(diào)用下面的方法
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSURL *webpageURL = userActivity.webpageURL;
NSLog(@"====== Universal Links啟動APP links:%@======", webpageURL.absoluteString);
if ([webpageURL.absoluteString containsString:@"test.com"]) {
//判斷域名是自己的網(wǎng)站,進行我們需要的處理
UINavigationController *nav;
if (self.tabBarController && self.tabBarController.viewControllers.count) {
NSInteger index = [self.tabBarController selectedIndex];
nav = [self.tabBarController.viewControllers objectAtIndex:index];
}else {
nav = nil;
}
//4.解決打開未啟動的app無法跳轉(zhuǎn)指定頁面的問題
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
//跳轉(zhuǎn)CDVViewController
[[MXKit shareMXKit] pushToUniversalLinksUrlString:webpageURL.absoluteString withNavController:nav withCallback:nil];
});
}
}
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//2.解決打開未啟動的app無法跳轉(zhuǎn)指定頁面的問題
dispatchGroup = dispatch_group_create();
dispatch_group_enter(dispatchGroup);
//doSomething...
//3.解決打開未啟動的app無法跳轉(zhuǎn)指定頁面的問題
dispatch_group_leave(dispatchGroup);
return YES;
}
這樣, 我們就實現(xiàn)了, 點擊
www.test.com
跳轉(zhuǎn)到app的功能了.