点击微信分享链接打开App

开始之前,准备一个测试数据。分享出去的其实是一个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_第1张图片
添加白名单.jpeg

到此为止,我们的准备数据工作就完成了。下面开始进入我们自己的App处理代码.......

part 1 约定scheme

点击微信分享链接打开App_第2张图片
scheme.jpeg

这里的scheme跟测试数据里面的appBUrl的前缀、LSApplicationQueriesSchemes是一样的~

part 2 代码处理部分

当App运行在前台时,点击分享链接进来在此处拿到URL

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)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];
}

part 3 效果图

效果图.gif

你可能感兴趣的:(点击微信分享链接打开App)