• 【转】使用URL SCHEME启动天猫客户端并跳转到某个商品页面的方法


    在项目中遇到了这样一个需求:让用户在手机应用中,点击一个天猫的商品链接(知道商品在PC浏览器里的地址),直接启动天猫的客户端并显示这个商品。以前曾经实现过类似的功能,不过那次是淘宝的商品,天猫和淘宝的客户端不同,参数也不一样,直接套淘宝的格式就不行了。不过,总体的思路还是类似的,就是使用iOS 的URL SCHEME机制。


    关于URL Scheme的基本原理,网上已经有很多详细讲解的帖子,这里就不重复了。不清楚的同学,看这个经典帖子就可以:http://www.cocoachina.com/newbie/tutorial/2012/0529/4302.html 我这里只说天猫的scheme参数格式。


    启动天猫客户端的url scheme格式:tmall://tmallclient/?{"action":”item:id=xxxxx”}
    其中xxxxx是商品的id。

    比如:某个天猫商品的http链接为:http://detail.tmall.com/item.htm?spm=a215v.7217581.610138.10.sO6nZp&id=36615660686&areaId=410100&cat_id=2&rn=ebe1860b08257aacbac424ed12d5208c&user_id=1098342976&is_b=1

    从中抽取”id=“后面的11位数字,此为商品ID,然后,通过下面的URL启动天猫客户端:
    tmall://tmallclient/?{"action":”item:id=36615660686”}



    贴段代码作为例子吧:

     
    复制代码
    1. NSString *urlString = @“http://detail.tmall.com/item.htm?spm=a215v.7217581.610138.10.sO6nZp&id=36615660686&areaId=410100&cat_id=2&rn=ebe1860b08257aacbac424ed12d5208c&user_id=1098342976&is_b=1”;
    2. NSURL *url;
    3. if([urlString rangeOfString:@"detail.tmall."].location != NSNotFound)   //判断Url是否是天猫商品的链接
    4. {
    5.     NSRange range = [urlString rangeOfString:@"id="]; //在URL中找到商品的ID
    6.     if(range.location != NSNotFound)
    7.     {
    8.         NSString *productID = [urlString substringWithRange:NSMakeRange(range.location + 3, 11)];
    9.         NSString *appUrl = [NSString stringWithFormat:@"tmall://tmallclient/?{"action":"item:id=%@"}", productID];
    10.         url = [NSURL URLWithString:[appUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    11.         if ([[UIApplication sharedApplication] canOpenURL:url])
    12.         {
    13.             // 如果已经安装天猫客户端,就使用客户端打开链接
    14.             [[UIApplication sharedApplication] openURL:url];
    15.         }
    16.         else
    17.         {
    18.             //客户手机上没有装天猫客户端,这时启动浏览器以网页的方式浏览该商品。
    19.             url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    20.             [[UIApplication sharedApplication] openURL:url];
    21.         }
    22.     }
    23. }


    from:http://www.cocoachina.com/bbs/read.php?tid=220610

  • 相关阅读:
    shell 学习笔记4-shell内置变量命令
    shell 学习笔记3-shell变量扩展
    vmware vSphere Data Protection 6.1 --------3-使用备份、恢复、报告
    shell 学习笔记2-shell-test
    vmware vSphere Data Protection 6.1--------2-初始化
    Centos7+puppet+foreman,模板介绍
    vmware vSphere Data Protection 6.1 --------1-部署
    vmware vcsa-故障1
    Grafana重置admin登录密码
    MongoDB远程连接报错
  • 原文地址:https://www.cnblogs.com/xuan52rock/p/6526179.html
Copyright © 2020-2023  润新知