DeepLink Kit
概述
DeepLink Kit 是一个出色的路由匹配、基于块的处理深链接方式。而不是决定如何格式化您的URL、解析它们、传递数据以及导航到特定内容或执行操作,这个库和一些代码行就可以让你入门。
看看这个
请运行以下命令以尝试运行 DeepLinkKit
示例项目:
pod try "DeepLinkKit"
安装
CocoaPods
DeepLinkKit可通过CocoaPods获取。要安装库,只需将以下行添加到您的Podfile中
pod "DeepLinkKit"
Carthage
要通过Carthage安装,请将以下行添加到您的Cartfile中
github "button/DeepLinkKit"
其他
如果您既不使用CocoaPods也不使用Carthage,可以将DeepLinkKit目录中的所有源文件包含到您的项目中去。
用法
按照以下简单步骤,在5分钟内或更短的时间内为您的应用程序添加深度链接支持。
注意:从1.0.0
版本开始,应将所有导入更新为导入<DeepLinkKit/DeepLinkKit.h>
。
1. 确保您的应用程序在Info.plist中注册了URL方案
2. 导入DeepLinkKit
#import <DeepLinkKit/DeepLinkKit.h>
3. 在您的应用程序代理中创建一个DPLDeepLinkRouter实例
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.router = [[DPLDeepLinkRouter alloc] init];
return YES;
}
4. 注册路由处理器
Objective-C
self.router[@"/log/:message"] = ^(DPLDeepLink *link) {
NSLog(@"%@", link.routeParameters[@"message"]);
};
Swift
self.router.register("/log/:message") { link in
if let link = link {
print("\(link.routeParameters["message"])")
}
}
5. 将传入的URL传递给路由器
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [self.router handleURL:url withCompletion:NULL];
}
6. 将NSUserActivity
对象传递给路由器(可选)
注意:如果您应用程序支持Apple的新通用链接,在应用程序代理中实现以下操作
- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:(void (^)(NSArray *))restorationHandler {
return [self.router handleUserActivity:userActivity withCompletion:NULL];
}
通过阅读我们的集成指南了解更多关于DeepLinkKit的信息。
路由注册示例
进入您应用程序的URL将具有以下类似的格式:<scheme>://<host>/<path-component>/<path-component>
在注册路由时,需要注意的是,您已注册的路由中的第一个正斜杠将决定要匹配的路径的开始。位于第一个正斜杠之前的路由组件将被视为主机。
例如,您有一个传入的URL为 twitter://timeline
// Matches the URL.
router[@"timeline"] = ^{ … }
// Does not match the URL.
router[@"/timeline"] = ^{ … }
另一例子,一个URL为 twitter://dpl.com/timeline
// Matches the URL.
router[@"/timeline"] = ^{ … }
// Does not match the URL.
router[@"timeline"] = ^{ … }
您还可以具体指定方案。如果您的应用程序支持多个URL方案,则可以注册针对这些方案的路由,如下所示
一个传入的URL为 scheme-one://timeline
// Matches the URL.
router[@"scheme-one://timeline"] = ^{ … }
// Does not match the URL.
router[@"scheme-two://timeline"] = ^{ … }
正则表达式路由匹配
您还可以在路由模式中使用正则表达式以获得最大灵活性。
匹配任何URL
以下将匹配所有传入的URL
router[@".*"] ^(DPLDeepLink *link){
// This will match all incoming links
}
注意:路由按照注册的顺序进行匹配,因此首先注册此路由将防止所有其他更具体的路由进行匹配。
匹配具有特定方案的URL
以下将匹配所有具有方案 myscheme://
的传入链接
router[@"myscheme://.*"] ^(DPLDeepLink *link){
// matches all urls with a scheme of `myscheme://`
}
您还可以命名正则表达式组
以下将匹配任何具有 host
为 trycaviar.com
的URL并将 :path
在路由参数中提供。
// Given the url ‘https://trycaviar.com/manhattan/nicoletta-297`
router[@"trycaviar.com/:path(.*)"] ^(DPLDeepLink *link){
// `link[@"path"]` => @"manhattan/nicoletta-297"
}
匹配多个路径组件
在这种情况下,您将得到路由参数中的 :city
和 :restaurant
。
// Given the url ‘https://trycaviar.com/manhattan/nicoletta-297`
router[@"trycaviar.com/:city([a-zA-Z]+)/:restaurant(.*)"] ^(DPLDeepLink *link){
// `link[@"city"]` => @"manhattan"
// `link[@"restaurant"]` => @"nicoletta-297"
}
如果餐厅ID是数字,则可以如下限制匹配。
// Given the url ‘https://trycaviar.com/manhattan/297`
router[@"trycaviar.com/:city([a-zA-Z]+)/:restaurant([0-9])"] ^(DPLDeepLink *link){
// `link[@"city"]` => @"manhattan"
// `link[@"restaurant"]` => @"297"
}
命名一些组而不是其他组
// Lets say the url is ‘https://trycaviar.com/manhattan/pizza/nicoletta-297`
router[@"trycaviar.com/:city([a-zA-Z]+)/[a-z]+/:restaurant(.*)"] ^(DPLDeepLink *link){
// `link[@"city"]` => @"manhattan"
// `link[@"restaurant"]` => @"nicoletta-297"
}
以上将匹配 “https://trycaviar.com/manhattan/pizza/nicoletta-297”,但不匹配 “https://trycaviar.com/manhattan/PIZZA/nicoletta-297” 或 “https://trycaviar.com/manhattan/pizza-places/nicoletta-297” 等。
AppLinks 支持
您的应用程序支持 AppLinks 吗?您可以通过导入 DPLDeepLink+AppLinks
类别轻松处理传入的 AppLinks。AppLinks 类别提供了对 AppLinks 1.0 所有属性的方便访问器。
router[@"/timeline"] = ^(DPLDeepLink *link) {
NSURL *referrerURL = link.referralURL;
NSString *someValue = link.extras[@"some-key"];
}
运行演示
要运行示例项目,请在您的终端中运行 pod try DeepLinkKit
。您还可以clone 仓库,并从项目根目录运行 pod install
。如果您没有 CocoaPods,请先 遵循此指南。
有两个演示应用程序,分别为 SenderDemo
和 ReceiverDemo
。 ReceiverDemo
已有一些注册路由,用于处理特定的深度链接。 SenderDemo
包含几个操作,可以将深度链接发送到 ReceiverDemo
进行满足。
首先运行SenderDemo
构建方案,然后停止模拟器,并将构建方案切换到ReceiverDemo
并再次运行。现在您可以将模拟器中的SenderDemo
应用切换回,并点击某个操作。
创建深度链接
您还可以使用DPLMutableDeepLink
创建深度链接。在两个集成有DeepLinkKit
的应用程序之间,您可以通过深度链接从应用程序中传递复杂对象到另一个应用程序,并轻松在另一端获得该对象。
在第一个应用程序中
DPLMutableDeepLink *link = [[DPLMutableDeepLink alloc] initWithString:@"app-two://categories"];
link[@"brew-types"] = @[@"Ale", @"Lager", @"Stout", @"Wheat"]
link[@"beers"] = @{
@"ales": @[
@{
@"name": @"Southern Tier Pumking Ale",
@"price": @799
},
@{
@"name": @"Sierra Nevada Celebration Ale",
@"price": @799
}
],
@"lagers": @[
...
],
...
}
[[UIApplication sharedApplication] openURL:link.URL];
在第二个应用程序中
router[@"categories"] = ^(DPLDeepLink *link) {
NSArray *brewTypes = link[@"brew-types"];
NSDictionary *beers = link[@"beers"];
}
作者
许可证
DeepLinkKit可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。
贡献
我们非常希望看到您对这个库改善的创意。最佳的贡献方式是提交一个pull请求。我们将尽力尽快回应您。您还可以提交一个新的GitHub问题,如果您发现错误或有问题。
请确保遵循我们的通用编码样式,并为新功能添加覆盖率测试!