好的東西就應該大家一塊分享。
今天項目中要求,類似於啟動頁是淘寶的廣告,然后點擊廣告,跳轉到淘寶的詳情頁。
實現這個要求我是各種百度,不過最后發現呢,大部分東西都是千篇一律。
第一種:只是提供了天貓的跳轉,並沒有提供淘寶的跳轉。
第二種:就是使用阿里百川的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]; } }
這樣就可以了,簡單大方。
-------
有同學反映說,為什么按照你的代碼寫了,但是不好使呢?
eeee,這個問題
要成功的實現跳轉,你需要做的工作 還有 就是需要在plist文件里邊的url 里邊分別添加上 淘寶 跟天貓的url
分別是 CFBundleURLTypes taobao - taobao tmall:// - tmall://
還有就是需要在下邊的
LSApplicationQueriesSchemes
taobao
tmall
果然 用心愛過的人不會忘,用心做過的程序記記也能記起來。
-----------跳轉京東客戶端
-(void)tojd:(NSString *)itemId{ //這個是京東店鋪的鏈接 // https://jinjinshang.jd.com //這個是京東的商品鏈接 // https://item.jd.com/4922289.html // // // 方法1 // URL詳情頁 NSString *qianUrl = @"openapp.jdmobile://virtual?params=%7B%22sourceValue%22:%220_productDetail_97%22,%22des%22:%22productDetail%22,%22skuId%22:%22"; NSString *skuid = @"4922289"; NSString *houUrl =@"%22,%22category%22:%22jump%22,%22sourceType%22:%22PCUBE_CHANNEL%22%7D"; NSString *urlString=[NSString stringWithFormat:@"%@%@%@",qianUrl,skuid,houUrl]; NSURL *url=[NSURL URLWithString:urlString]; [[UIApplication sharedApplication] openURL:url]; // 判斷當前系統是否有安裝淘寶客戶端 if ([[UIApplication sharedApplication] canOpenURL:url]) { // 如果已經安裝淘寶客戶端,就使用客戶端打開鏈接 [[UIApplication sharedApplication] openURL:url]; } else { // 否則使用 Mobile Safari 或者內嵌 WebView 來顯示 NSString *url = [NSString stringWithFormat:@"http://item.taobao.com/item.htm?id=%@",itemId]; LSH5VC *vc = [[LSH5VC alloc]init]; vc.webUrl = url; [self.navigationController pushViewController:vc animated:YES]; } }