URL Scheme

前言

由于苹果推出了“沙盒机制”,各个APP之间不能互相访问,但是我们在使用时经常会出现APP之间跳转的情况,这就是URL Scheme的用处了

简介

URL Scheme类似于URL,可以通过该APP的URL Scheme来定位到该APP,当然定位只是最基本的用法

概览

things:///add?title=正文内容&note=备注

  1. 连接头:things,它是启动一个应用的URL,比如:weixin
  2. 连接头和命令之间的:,还有//或者///
  3. 动作:add,指打开该APP后进行的动作
  4. 动作和参数之间的?
  5. 参数:titlenote
  6. 参数和值之间同样使用=
  7. 值:正文内容和备注(参数和值的使用类似于URL)
  8. 一组参数和另一组参数之间是&

细则

  1. Scheme
    对于URL来说它就是协议,但是在iOS里就是启动一个应用的URL,如果单纯只是想启动一个应用的URL使用“连接头+冒号”就可以,例如:weixin:
  2. 斜线数量
    举个例子,我在某平台登录了AB两个账号,现在登的是A号,但是想使用B号发布文章,就可以用things://B/post,将会直接使用B号并且在发布文章页面,而如果是things:///post,就不会切换到B号。也就是说/////的一种省略使用场景的形式

URL Scheme的用法

  1. 添加
    在这里插入图片描述

  2. 使用

查看用户是否安装该APP

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"***://***"]]) { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"***://***"]]; 
}else{ 
     //提示用户未安装app 
}

如果其他APP打开了自己的APP,也会触发自己的代理方法

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url NS_DEPRECATED_IOS(2_0, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED; 
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED; 
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options NS_AVAILABLE_IOS(9_0); // no equiv. notification. return NO if the application can't open for some reason

为了防止APP疯狂遍历用户手机上的APP,窥探隐私,iOS 9后苹果为URL Scheme添加了白名单,开发者需要在白名单中注册自己APP要用到的URL Scheme,而白名单是有数量限制的,最多50个,在iOS 9中不在白名单中注册的话,利用URL Scheme是打不开其他APP的

参考文献

入门 iOS 自动化:读懂 URL Schemes
iOS URL Scheme

URL Scheme 是统一资源定位符(Uniform Resource Locator, URL)的一部分,用于指定资源访问的协议。URL 通常由以下几个部分组成: - **Scheme**:定义访问资源所使用的协议,例如 `http`、`https`、`ftp`、`mailto` 等。 - **Authority**:包括域名或 IP 地址以及可选的端口号。 - **Path**:资源在服务器上的路径。 - **Query**:附加的查询参数。 - **Fragment**:页面内的锚点标识。 ### URL Scheme 的定义 URL Scheme 的语法通常遵循以下格式: ```text <scheme>://<authority>[/<path>][?<query>][#<fragment>] ``` 例如,`https://www.example.com/path/to/resource?query=1#fragment` 中的 Scheme 是 `https`。 ### URL Scheme 的实现 在不同编程语言和框架中,URL Scheme 可以通过解析字符串来实现,并提取各个部分的值。以下是一些常见语言的实现示例。 #### Python 示例 Python 提供了 `urllib.parse` 模块来解析 URL 并提取 Scheme。 ```python from urllib.parse import urlparse url = "https://www.example.com/path/to/resource?query=1#fragment" parsed_url = urlparse(url) print("Scheme:", parsed_url.scheme) # 输出: https print("Netloc:", parsed_url.netloc) # 输出: www.example.com print("Path:", parsed_url.path) # 输出: /path/to/resource print("Query:", parsed_url.query) # 输出: query=1 print("Fragment:", parsed_url.fragment) # 输出: fragment ``` #### JavaScript 示例 在 JavaScript 中,可以通过 `URL` 对象来解析 URL 并提取 Scheme。 ```javascript const url = new URL("https://www.example.com/path/to/resource?query=1#fragment"); console.log("Scheme:", url.protocol); // 输出: https: console.log("Hostname:", url.hostname); // 输出: www.example.com console.log("Path:", url.pathname); // 输出: /path/to/resource console.log("Search:", url.search); // 输出: ?query=1 console.log("Hash:", url.hash); // 输出: #fragment ``` #### Java 示例 Java 提供了 `java.net.URL` 类来解析 URL 并提取 Scheme。 ```java import java.net.URL; public class Main { public static void main(String[] args) throws Exception { URL url = new URL("https://www.example.com/path/to/resource?query=1#fragment"); System.out.println("Scheme: " + url.getProtocol()); // 输出: https System.out.println("Host: " + url.getHost()); // 输出: www.example.com System.out.println("Path: " + url.getPath()); // 输出: /path/to/resource System.out.println("Query: " + url.getQuery()); // 输出: query=1 System.out.println("Ref: " + url.getRef()); // 输出: fragment } } ``` ### 自定义 URL Scheme 除了标准的 URL Scheme(如 `http` 和 `https`),某些应用程序可以定义自己的 Scheme。例如,移动应用程序可以注册自定义的 URL Scheme(如 `myapp://`),以便通过链接直接启动应用程序。 #### Android 中的自定义 Scheme 在 Android 中,可以通过在 `AndroidManifest.xml` 文件中添加 `intent-filter` 来定义自定义 Scheme。 ```xml <activity android:name=".MyActivity"> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" android:host="example.com" /> </intent-filter> </activity> ``` #### iOS 中的自定义 Scheme 在 iOS 中,可以通过在 `Info.plist` 文件中添加 `LSApplicationQueriesSchemes` 来定义自定义 Scheme。 ```xml <key>LSApplicationQueriesSchemes</key> <array> <string>myapp</string> </array> ``` ### 总结 URL SchemeURL 的关键组成部分,用于指定访问资源的协议。通过解析 URL,可以提取 Scheme 以及其他部分(如 Authority、Path、Query 和 Fragment)。不同编程语言和平台提供了相应的工具来实现和处理 URL Scheme
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值