✌️工具类又多了一个方法。下面。
嗯~ 过程中遇到的问题也已解决。
Plist文件中添加
LSApplicationQueriesSchemes
openapp.jdMobile 京东
tmall 天猫
taobao 淘宝
#pragma mark - 打开 淘宝、天猫、京东
+(void)showItemInTmall4iOS:(NSString *)string
{
NSURL *url;
if([string rangeOfString:@".tmall."].location != NSNotFound)//判断Url是否是天猫商品的链接
{
NSRange range = [string rangeOfString:@"id="]; //在URL中找到商品的ID
if(range.location != NSNotFound){
NSString *productID = [string 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:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
}
}else if ([string rangeOfString:@"item.taobao.com"].location != NSNotFound){//判断是否是淘宝
// 构建淘宝客户端协议的 URL
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"taobao://item.taobao.com/item.htm?id=%@", string]];
// 判断当前系统是否有安装淘宝客户端
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=%@", string]];
[[UIApplication sharedApplication] openURL:url];
}
}else if([string rangeOfString:@"item.jd.com"].location != NSNotFound){
//截取商品的ID
NSRange range1 = [string rangeOfString:@"https://item.jd.com/"];
NSString *productID = [string substringFromIndex:range1.location + range1.length];
NSRange range2 = [productID rangeOfString:@".html"];
productID = [productID substringToIndex:range2.location];
NSString *urlString=[NSString stringWithFormat:@"openApp.jdMobile://virtual?params={\"category\":\"jump\",\"des\":\"productDetail\",\"skuId\":\"%@\",\"sourceType\":\"JSHOP_SOURCE_TYPE\",\"sourceValue\":\"JSHOP_SOURCE_VALUE\"}",productID];
//京东打开
url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if([[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL:url];
}else{
// 打开京东下载连接
NSURL * url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/shou-ji-jing-dong-xin-ren/id414245413?l=en&;mt=8"];
[[UIApplication sharedApplication] openURL:url];
}
}
}