说说iOS开发中的Deeplink吧

好久没更新了,过去的一年发生了很多事情,无论是于我还是于整个移动端来说,希望大家能跟上移动端发展的脚步吧,不断变化也是一个不断学习的过程,各位移动端的小伙伴加油。
Deeplink的概念就不多说了,直接说说怎么用吧。
1:首先要定义好协议:
如:youappname://youwebnet.com?target=targetpage
2:配置schemes:
参考这位仁兄的博客:http://blog.csdn.net/qq_25224791/article/details/73089832

3:appA 和 appB之间的跳转:

             协议:appA://superman.com?target=kucun
                  appB://superman.com?target=shoppingvc

4:场景一:appA页面跳转到appB的shoppingvc页面,shoppingvc对应的是WGMallViewController,因为我这里使用的是路由跳转的方式):


QQ20180109-0.png

在appB的delegate中实现:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    NSString *scheme = url.scheme;
    NSString *host = url.host;
    NSString *query = url.query;
    if ([url.scheme isEqualToString:@"appB"]) {
        //deeplink跳转协议 :com.deeplink://deeplink.htm?id=123456 com.deeplink 代表就是设置的 Url Schemes =>example:appB://superman.com?target=shoppingvc
        //取出target跳转
        if (query.length) {
            NSString *targetVC = [query componentsSeparatedByString:@"="].lastObject;
            //targetVC是shoppingvc,shopping page是跳转到该页面时设置的标题
            NSString *pushURL = [NSString stringWithFormat:@"/%@/%@",targetVC,@"shopping page"];
            //使用VXRouter直接push到目标页面
            [[VXRouter shared] pushURLString:pushURL animated:YES];
            return YES;
        }
        return false;
        
    } 
    
    return false;
}

在appA实现点击跳转事件:

- (void)pushTargetBtnAction {
    NSString *urlString = @"appB://superman.com?target=shoppingvc";
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
        //若安装了appB则跳转到APP
        NSURL * url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [[UIApplication sharedApplication] openURL:url];
    }else{
        //如果没安装则跳转到下载页面(此处跳转到企业版下载界面)
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-services://?action=download-manifest&url=https://xxxxxx.qnssl.com/xxx.plist"]];
    }
}

此处有坑请注意,如果你发现你运行的时候不走以下方法 (·······这是一个尴尬的标签,怎么不变粗)

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

那么请在以下方法中实现:

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options

5:使用场景二:从appA的web页面跳转到appA指定的kucun原生页面:
协议: appA://superman.com?target=kucunu

 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    NSString *scheme = url.scheme;
    NSString *host = url.host;
    NSString *query = url.query;
    if ([url.scheme isEqualToString:@"appA"]) {
        //取出target跳转
        if (query.length) {
            NSString *targetVC = [query componentsSeparatedByString:@"="].lastObject;
            NSString *pushURL = [NSString stringWithFormat:@"/%@/%@",targetVC,@"kucun page"];
            [[VXRouter shared] pushURLString:pushURL animated:YES];
            return YES;
        }
        return false;
        
    } 
    
    return false;
}

数据可以通过http get拼接的方式传递,从

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

中去解析协议,把数据用json的形式使用路由的方式传递到目标页面,在目标页面去解析数据,刷新页面,路由传递数据请看我之前的博文:https://www.jianshu.com/p/9e29c6d9983a

你可能感兴趣的:(说说iOS开发中的Deeplink吧)