iOS APP调用天猫or淘宝客户端

这段代码展示了如何在iOS应用中根据参数调用天猫或淘宝客户端来显示商品详情。如果客户端未安装,则会跳转到网页版进行浏览。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (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];
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值