// // CommonTaoBao.m // sdjy // // Created by Virgil on 2017/9/26. // Copyright © 2017年 Virgil. All rights reserved. // #import "CommonTaoBao.h" @implementation CommonTaoBao + (void)showItemInWebOrAppWithTypeFrom:(NSString *)typeFrom urlstr:(NSString *)url { if ([typeFrom isEqualToString:@"tmall"]) { [self showItemInTmall4iOS:url]; } else if([typeFrom isEqualToString:@"taobao"]) { [self showItemInTaobao4iOS:url]; }else{ [self tongwanWeb:[NSURL URLWithString:url]]; } } + (void)showItemInTmall4iOS:(NSString *)itemId { NSURL *url; if([itemId rangeOfString:@"detail.tmall."].location != NSNotFound) //判断Url是否是天猫商品的链接 { NSRange range = [itemId rangeOfString:@"id="]; //在URL中找到商品的ID if(range.location != NSNotFound) { NSString *productID = [itemId substringWithRange:NSMakeRange(range.location + 3, 11)]; NSString *appUrl = [NSString stringWithFormat:@"tmall://tmallclient/?{\"action\":\"item:id=%@\"}", productID]; url = [NSURL URLWithString:[appUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; if ([[UIApplication sharedApplication] canOpenURL:url]) { // 如果已经安装天猫客户端,就使用客户端打开链接 [[UIApplication sharedApplication] openURL:url]; } else { //客户手机上没有装天猫客户端,这时启动浏览器以网页的方式浏览该商品。 url = [NSURL URLWithString:[itemId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url]; } } } } + (void)showItemInTaobao4iOS:(NSString *)itemId { // 构建淘宝客户端协议的 URL NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"taobao://item.taobao.com/item.htm?id=%@", itemId]]; // 判断当前系统是否有安装淘宝客户端 if ([[UIApplication sharedApplication] canOpenURL:url]) { // 如果已经安装淘宝客户端,就使用客户端打开链接 [[UIApplication sharedApplication] openURL:url]; } else { // 否则使用 Mobile Safari 或者内嵌 WebView 来显示 url = [NSURL URLWithString:[NSString stringWithFormat:@"http://item.taobao.com/item.htm?id=%@", itemId]]; // [[UIApplication sharedApplication] openURL:url]; [self tongwanWeb:url]; } } + (void)tongwanWeb:(NSURL *)url { [[UIApplication sharedApplication] openURL:url]; } @end