开始之前,准备一个测试数据。分享出去的其实是一个URL,我们这边呢简单写个小demo就好。
- (IBAction)toEat:(UIButton *)sender {
// 1.获取应用程序App-B的URL Scheme
/*
biz 业务类型。1-购物
et 页面。 1-单品页
*/
NSURL *appBUrl = [NSURL URLWithString:[NSString stringWithFormat:@"ue://storeID=%zd&productID=1&biz=1&et=1",sender.tag]];
// 2.判断手机中是否安装了对应程序
if ([[UIApplication sharedApplication] canOpenURL:appBUrl]) {
// 3. 打开应用程序App-B
[[UIApplication sharedApplication] openURL:appBUrl];
} else {
NSLog(@"没有安装");
}
}
涉及到跳转,我们这边需要给一个白名单LSApplicationQueriesSchemes
到此为止,我们的准备数据工作就完成了。下面开始进入我们自己的App处理代码.......
part 1 约定scheme
这里的scheme
跟测试数据里面的appBUrl
的前缀、LSApplicationQueriesSchemes
是一样的~
part 2 代码处理部分
当App运行在前台时,点击分享链接进来在此处拿到URL
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
NSString *string =[url absoluteString];
if ([string hasPrefix:@"ue"])
{
self.tab.selectedIndex = 0;
//点击分享进来
[self postShareNotiWithUrlStr:string];
}
return YES;
}
当App被杀死时,点击分享链接进来在此处拿到URL
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
self.tab = [JKTabBarController new];
self.window.rootViewController = self.tab;
[self.window makeKeyAndVisible];
//程序杀死后获取分享url
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
NSString *string =[url absoluteString];
if ([string hasPrefix:@"ue"])
{
[self postShareNotiWithUrlStr:string];
}
return YES;
}
拿到URL之后的处理
-(void)postShareNotiWithUrlStr:(NSString *)urlStr{
NSRange range = [urlStr rangeOfString:@"://"];
NSString *paramStr = [urlStr substringFromIndex:range.location + range.length];
NSArray *arrA = [paramStr componentsSeparatedByString:@"&"];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for (int i = 0; i < arrA.count; i++) {
NSArray *arrB = [arrA[i] componentsSeparatedByString:@"="];
[dic setValue:arrB.lastObject forKey:arrB.firstObject];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//发送通知去首页
[[NSNotificationCenter defaultCenter] postNotificationName:@"toHome" object:nil userInfo:dic];
});
}
首页逻辑的处理
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toDetailVC:) name:@"toHome" object:nil];
}
-(void)toDetailVC:(NSNotification *)noti{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"toHome" object:nil];
JKCommodityDetailsController *commodityDetailsController = [[JKCommodityDetailsController alloc] init];
NSDictionary *dic = noti.userInfo;
if ([[dic valueForKey:@"biz"] isEqualToString:@"1"] && [[dic valueForKey:@"et"] isEqualToString:@"1"])
{
commodityDetailsController.storeID = [dic valueForKey:@"storeID"];
commodityDetailsController.productId = [[dic valueForKey:@"productID"] integerValue];
commodityDetailsController.SourceType = SourceTypeShare;
}
[self.navigationController pushViewController:commodityDetailsController animated:YES];
}