关于URL SCHEME

天猫


使用URL SCHEME启动天猫客户端并跳转到某个商品页面的方法   





在项目中遇到了这样一个需求:让用户在手机应用中,点击一个天猫的商品链接(知道商品在PC浏览器里的地址),直接启动天猫的客户端并显示这个商品。以前曾经实现过类似的功能,不过那次是淘宝的商品,天猫和淘宝的客户端不同,参数也不一样,直接套淘宝的格式就不行了。不过,总体的思路还是类似的,就是使用iOS URL SCHEME机制。



关于URL Scheme的基本原理,网上已经有很多详细讲解的帖子,这里就不重复了。不清楚的同学,看这个经典帖子就可以:http://www.cocoachina.com/newbie/tutorial/2012/0529/4302.html 我这里只说天猫的scheme参数格式。



启动天猫客户端url scheme格式:tmall://tmallclient/?{"action":”item:id=xxxxx”}

其中xxxxx是商品的id


比如:某个天猫商品的http链接为:http://detail.tmall.com/item.htm?spm=a215v.7217581.610138.10.sO6nZp&id=36615660686&areaId=410100&cat_id=2&rn=ebe1860b08257aacbac424ed12d5208c&user_id=1098342976&is_b=1


从中抽取”id=“后面的11位数字,此为商品ID,然后,通过下面的URL启动天猫客户端:

tmall://tmallclient/?{"action":”item:id=36615660686”}




贴段代码作为例子吧:



复制代码

  1. NSString *urlString = @“http://detail.tmall.com/item.htm?spm=a215v.7217581.610138.10.sO6nZp&id=36615660686&areaId=410100&cat_id=2&rn=ebe1860b08257aacbac424ed12d5208c&user_id=1098342976&is_b=1”;
  2. NSURL *url;
  3. if([urlString rangeOfString:@"detail.tmall."].location != NSNotFound)   //判断Url是否是天猫商品的链接
  4. {
  5.     NSRange range = [urlString rangeOfString:@"id="]; //URL中找到商品的ID
  6.     if(range.location != NSNotFound)
  7.     {
  8.         NSString *productID = [urlString substringWithRange:NSMakeRange(range.location + 3, 11)];
  9.         NSString *appUrl = [NSString stringWithFormat:@"tmall://tmallclient/?{\"action\":\"item:id=%@\"}", productID];
  10.         url = [NSURL URLWithString:[appUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  11.         if ([[UIApplication sharedApplication] canOpenURL:url])
  12.         {
  13.             // 如果已经安装天猫客户端,就使用客户端打开链接
  14.             [[UIApplication sharedApplication] openURL:url];
  15.         }
  16.         else
  17.         {
  18.             //客户手机上没有装天猫客户端,这时启动浏览器以网页的方式浏览该商品。
  19.             url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  20.             [[UIApplication sharedApplication] openURL:url];
  21.         }
  22.     }
  23. }



我使用如下代码为什么没有成功跳转,打印结果是0,求指教


NSString *string = @"tmall://tmallclient/?{\"action\":\"item:id=36930336665\"}";

NSURL *url = [NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

BOOL aa = [[UIApplication sharedApplication] canOpenURL:url];

NSLog(@"%d",aa);

[[UIApplication sharedApplication] openURL:url];


PS:刚刚安装了客户端,发现是可以调用的,好像就是不能在浏览器打开,稍候需要继续看下



<think>我们正在处理用户关于工程相机URLScheme的查询。根据引用[1],我们知道在iOS中注册URLScheme需要在info.plist中添加URLtypes。引用[3]提到了在Android中调用相机的代码片段,但用户的问题更可能集中在iOS的URLScheme上,因为引用[1]和[4]都涉及iOS的URL机制。引用[4]介绍了UniversalLink,但用户明确询问的是URLScheme。因此,我们将重点放在iOS上如何为工程相机应用定义和使用URLScheme。步骤包括:1.在工程相机的Xcode项目中注册URLScheme。2.在工程相机中处理传入的URL请求。3.其他应用通过这个URLScheme调用工程相机。注意:用户问题中的“工程相机”可能是一个具体的相机应用,但这里我们假设是一个自定义的相机应用,因为用户没有指定特定的第三方应用。具体步骤:###1.注册URLScheme(iOS)在Xcode项目中:-打开`Info.plist`。-添加`URLtypes`(或`CFBundleURLTypes`)键。-在`URLtypes`下添加一个字典,包含`URLidentifier`(通常使用反向域名)和`URLSchemes`(一个数组,包含你的URLScheme字符串)。例如,为工程相机注册一个URLScheme为`engineercamera`:```xml<key>CFBundleURLTypes</key><array><dict><key>CFBundleTypeRole</key><string>Editor</string><key>CFBundleURLName</key><string>com.yourcompany.engineercamera</string><key>CFBundleURLSchemes</key><array><string>engineercamera</string></array></dict></array>```###2.处理URL请求(iOS)在工程相机App中,需要实现处理传入URL的逻辑。通常在AppDelegate中实现`application(_:open:options:)`方法(对于iOS13之前,也可能是`application(_:open:sourceApplication:annotation:)`)。示例(Swift):```swiftfuncapplication(_app:UIApplication,openurl:URL,options:[UIApplication.OpenURLOptionsKey:Any]=[:])->Bool{//解析URL,根据URL执行相应操作ifurl.scheme=="engineercamera"{//例如,解析URL中的参数,然后启动相机并设置参数//这里可以处理不同的命令,比如启动某种模式returntrue}returnfalse}```###3.在其他应用中调用工程相机其他应用可以通过打开特定的URL来调用工程相机。例如,在另一个应用中,使用以下代码(Swift):```swiftifleturl=URL(string:"engineercamera://mode/scan"){ifUIApplication.shared.canOpenURL(url){UIApplication.shared.open(url,options:[:],completionHandler:nil)}else{//处理工程相机未安装的情况}}```这里,我们假设通过URL传递了模式参数(`mode/scan`),在工程相机中需要解析这个URL以确定要执行的操作。###注意事项-避免使用系统预留的URLScheme(如`http`,`https`,`tel`等),以免冲突(引用[1])。-在Android中,也可以通过Intent来调用其他应用,但用户的问题集中在URLScheme,而URLScheme在iOS中更常见。对于Android,可以使用自定义的IntentFilter(类似)。###引用说明我们参考了引用[1]中关于注册URLScheme的方法,以及引用[4]中关于URLScheme与UniversalLink的对比。用户的问题没有涉及UniversalLink,所以我们只讨论URLScheme的实现。###生成相关问题根据用户的查询,我们生成以下相关问题:§§相关问题§§1.如何为工程相机应用定义Android的IntentFilter?2.在iOS中,如何处理通过URLScheme传递的参数?3.工程相机应用如何在没有安装时跳转到AppStore?4.如何避免自定义URLScheme被其他应用恶意调用?5.工程相机应用如何支持UniversalLink?</think>### 工程相机 URL Scheme 实现与使用指南 #### 一、iOS 实现方式 1. **注册 URL Scheme** - 在工程相机的 `Info.plist` 中添加 URL Types: ```xml <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>com.yourcompany.engineercamera</string> <key>CFBundleURLSchemes</key> <array> <string>engineercamera</string> <!-- 自定义协议名 --> </array> </dict> </array> ``` - 注意避开系统保留协议名(如 `tel`, `mailto`)[^1] 2. **处理 URL 请求** - 在 `AppDelegate` 中实现 URL 处理: ```swift func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { // 解析 URL 参数示例:engineercamera://capture?mode=blueprint if url.scheme == "engineercamera" { let params = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems // 执行相机操作(如启动特定拍摄模式) return true } return false } ``` #### 二、Android 实现方式 1. **声明 Intent Filter** - 在 `AndroidManifest.xml` 中注册: ```xml <activity android:name=".CameraActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="engineercamera" /> </intent-filter> </activity> ``` 2. **处理 Intent 数据** ```java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); if(intent.getData() != null && "engineercamera".equals(intent.getScheme())) { Uri uri = intent.getData(); String mode = uri.getQueryParameter("mode"); // 获取参数 startCameraWithMode(mode); } } ``` #### 三、调用示例 1. **Web 页面调用** ```html <a href="engineercamera://scan?project=BuildingA">启动工程扫描</a> ``` 2. **App 间调用 (iOS)** ```swift if let url = URL(string: "engineercamera://measure?unit=cm") { if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) } else { // 跳转到 App Store 下载 } } ``` 3. **Android 调用** ```java Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("engineercamera://capture?type=blueprint")); startActivity(intent); ``` #### 四、最佳实践 1. **参数规范** - 推荐使用标准参数格式:`scheme://host/path?key1=value1&key2=value2` - 示例:`engineercamera://scan/3d?precision=high` 2. **错误处理** - 检测未安装应用时跳转下载页 - 添加版本兼容参数:`engineercamera://...&v=2` 3. **安全措施** - 验证调用来源(使用 `sourceApplication` 参数) - 限制敏感操作(如删除数据需二次确认)[^4] > **注意**:iOS 9+ 推荐使用 [Universal Links](https://developer.apple.com/ios/universal-links/) 替代 URL Scheme 以获得更好的安全性和用户体验[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值