好的东西就应该大家一块分享。
今天项目中要求,类似于启动页是淘宝的广告,然后点击广告,跳转到淘宝的详情页。
实现这个要求我是各种百度,不过最后发现呢,大部分东西都是千篇一律。
第一种:只是提供了天猫的跳转,并没有提供淘宝的跳转。
第二种:就是使用阿里百川的sdk,这样的话对我我来说是觉得有点大材小用的毕竟只是一个广告页而已。
第三种:就是我通过不懈的努力,终于被我给发现了。
现在我就要记录下来。
- (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];
}
}
这样就可以了,简单大方。
尊重劳动成果:原文链接:http://lib.csdn.net/snippet/ios/42450