DeepLinksHandler
DeepLinksHandler
是处理项目中内部和外部 URL 的最简单方式!
演示
我在捆绑示例项目中提供了一些演示。要使它们生效,您必须首先使用 CocoaPods 运行项目依赖安装
pod install
安装
使用酷炫的 CocoaPods 将 DeepLinksHandler
添加到您的项目中
pod 'DeepLinksHandler'
用法
为了符合风格类型,使用以下格式的 URL
deeplinkshandler://inapp_am?type=subscription&productID=com.examplellc.dlh.7days
其中
scheme - deeplinkshandler
, host - inapp_am
, query - type=subscription&productID=com.examplellc.dlh.7days
如果您不需要配置复杂的操作,可以使用没有 query
的 URL
deeplinkshandler://show_subscription_screen
版本 1.0.0 - 1.1.0
一个特殊的案例 - 当应用没有启动时处理外部 URL。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSURL *url = launchOptions[UIApplicationLaunchOptionsURLKey];
if (url) {
[DeepLinksHandler handleURL:url withBlock:^(NSURL *url) {
NSLog(@"Your deelpink is handled");
}];
// this 'dispatch_after' necessary to handle your block after swizzling, which happens after [UIApplication sharedApplication] != nil
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[application openURL:url options:@{} completionHandler:nil];
});
}
return YES;
}
在任何使用情况下,您都应该在使用前设置特殊URL的句柄块。
!!!注意: 对于唯一的URL,只执行最后一个发送的块。
static NSString * const kTestHandleURL = @"testurl://viewcontroller?title=ExampleAlert&description=ExampleDescriptionAlert";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[DeepLinksHandler handleURL:[NSURL URLWithString:kTestHandleURL] withBlock:^(NSURL *url) {
NSString *title = nil;
NSString *description = nil;
for (NSURLQueryItem *item in [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:YES].queryItems)
{
if ([item.name isEqualToString:@"title"])
title = item.value;
else if ([item.name isEqualToString:@"description"])
description = item.value;
}
if (title && description) {
[[[UIAlertView alloc] initWithTitle:title message:description delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil] show];
}
}];
}
- (IBAction)buttonAction:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:kTestHandleURL] options:@{} completionHandler:nil];
}
@end
许可
MIT