iOS

最近做iOS项目的时候遇到一个需求,点击广告页,跳转到淘宝或天猫的商品详情页。

具体需要是这样:

1)安装了淘宝:跳转到淘宝详情页。

2)没装淘宝,装了天猫:跳转到天猫详情页

3)淘宝、天猫都没装:跳转到应用自己的页面,加载网页。

或者跳转到Safari浏览器

1)可以使用阿里百川的SDK完成。  文档地址:http://baichuan.taobao.com/doc2/detail.htm?spm=a3c0d.7629140.0.0.fXnfzs&treeId=51&articleId=102860&docType=1

2)拿到淘宝、天猫url scheme及相应的链接,自己完成跳转。代码如下:

  

  //loadURL 网店地址  loadURL 网店地址不加  http://  可以前后台商议 此为不加方法

    //NSURL *taobaoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"taobao://item.taobao.com/item.htm?id=%@", book_id]];

    //NSURL *tmallUrl = [NSURL URLWithString:[[NSString stringWithFormat:@"tmall://tmallclient/?{"action":"item:id=%@"}", book_id] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURL *taobaoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"taobao://@", loadURL]];

    NSURL *tmallUrl = [NSURL URLWithString:[[NSString stringWithFormat:@"tmall://%@",loadURL}",       book_id] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURL *defaultUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", loadURL]];

    if ([[UIApplication sharedApplication] canOpenURL:taobaoUrl]) {

        //能打开淘宝就打开淘宝

        [[UIApplication sharedApplication] openURL:taobaoUrl];

    } else if ([[UIApplication sharedApplication] canOpenURL:tmallUrl]) {

        //能打开天猫就打开天猫

        [[UIApplication sharedApplication] openURL:tmallUrl];

    } else {

        //都打不开就加载自己的网页

       /*

        KGAdWebVC *adWebVC = [[KGAdWebVC alloc] init];

        adWebVC.url = splash_url;

        [self.navigationController pushViewController:adWebVC animated:YES];

        */

       //跳浏览器

       [[UIApplication sharedApplication] openURL:tmallUrl];

 

    }

当用户从外部浏览器(如:UC、Safari)点击有关app的按钮时,实现一下功能

1、用户未安装app,点击按钮跳转到app的下载页面

2、用户已安装app,点击按钮打开app并显示指定页面(如注册,登录等)

首先我们给app添加一个URL Types,用于给web打开的接口


 

然后我们在web的代码中添加一下脚本即可


添加完以后,测试结果如下:

1、如果用户未安装app,点击按钮跳转到app的下载页面

2、用户已安装app,点击按钮只能打开app

二、实现跳转到app指定页面

在web页面里设置参数,

如果要实现跳转到指定页面,就需要传 参数 表示跳转到哪个页面,只需要在上面的网址里:即 ifr.src = 'com.zttd.webApp//参数'   

例如跳到登录页面:ifr.src = 'com.zttd.webApp//Login'

在APP里接收该参数,并解析进行跳转,需要在AppDelegate文件里实现以下两种方法,最好两种都写,否则可能接收不到数据,接收到该参数后,进行解析通过通知并跳转到指定页面


原文地址:https://www.cnblogs.com/baitongtong/p/5828828.html