OGeek|极客世界-中国程序员成长平台

标题: iOS 深度链接 : how to embed URL in another URL? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:35
标题: iOS 深度链接 : how to embed URL in another URL?

我需要使用另一个 URL 深层链接到我的应用,假设我的自定义方案是:

myapp://

那么深层链接 URL 如下所示:

myapp://?type=url&url=[another URL]

问题是“另一个网址”可能包含分隔符,如?、/、&等,我考虑过使用引号:

myapp://?type=url&url="http://another.url"

但是另外一个网址也可能有引号,请问有什么办法吗?

谢谢



Best Answer-推荐答案


您需要正确转义第二个 URL 中的所有特殊字符。我使用我写的一个实用方法来提供帮助:

+ (NSString *)encodeStringNSString *)string {
    NSString *result = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR("% '\"?=&+<>;:-#\\/~`!"), kCFStringEncodingUTF8);

    return result;
}

有了它(在某个辅助类中或添加到某个类别中),您可以执行以下操作:

NSString *link = @"http://another.url";
NSString *escapedLink = [SomeUtility encodeString:link];

NSString *myURL = [NSString stringWithFormat"myapp://?type=url&url=%@", escapedLink];

请注意,此 encodeString: 方法应用于正确转义您可能用于构建 URL 查询字符串的任何 URL 参数值。

关于iOS 深度链接 : how to embed URL in another URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18200401/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4