当前位置:  开发笔记 > 前端 > 正文

iOS:URL字符串中的&符问题

如何解决《iOS:URL字符串中的&符问题》经验,为你挑选了2个好方法。

我们如何在URL中传递字符串Mr.X和Mr.Y.

我试过这个,但这个人做了所有的字符,但是&符号.

[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

Holtwick.. 36

甚至更短:

@implementation NSString (Escaping)
- (NSString*)stringWithPercentEscape {            
    return [(NSString *) CFURLCreateStringByAddingPercentEscapes(
        NULL, 
        (CFStringRef)[[self mutableCopy] autorelease], 
        NULL, 
        CFSTR("?=,!$&'()*+;@?\n\"<>#\t :/"),
        kCFStringEncodingUTF8) autorelease];
}
@end

在这里它再次作为ARC符合内联函数帮助器:

#if __has_feature(objc_arc)
static inline NSString *hxURLEscape(NSString *v) {
    static CFStringRef _hxURLEscapeChars = CFSTR("?=,!$&'()*+;@?\r\n\"<>#\t :/");
    return ((__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
        NULL, 
        (__bridge CFStringRef)[v mutableCopy], 
        NULL, 
        _hxURLEscapeChars, 
        kCFStringEncodingUTF8));
}
#endif

更短更可怕:) (2认同)

实际上,我认为我们根本不需要复制它,可以用`self`替换`[[self mutableCopy] autorelease]`.关于它的更多细节可以在`CFURLCreateStringByAddingPercentEscapes`的文档中找到http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFURLRef/Reference/reference.html (2认同)


Rog.. 32

-stringByAddingPercentEscapesUsingEncoding:不执行完整的转义编码.您应该使用手动添加编码-replaceOccurrencesOfString:withString:

这是一个完整的列表(镜像Gamecat的列表),最初建议在https://devforums.apple.com/message/15674#15674.正如尼克指出的那样,这很昂贵,所以不要只考虑完整列表而不考虑你逃避的真正要求.

NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];   
[escaped replaceOccurrencesOfString:@"$" withString:@"%24" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];

此代码肯定会对您的URL进行哈希处理.



1> Holtwick..:

甚至更短:

@implementation NSString (Escaping)
- (NSString*)stringWithPercentEscape {            
    return [(NSString *) CFURLCreateStringByAddingPercentEscapes(
        NULL, 
        (CFStringRef)[[self mutableCopy] autorelease], 
        NULL, 
        CFSTR("?=,!$&'()*+;@?\n\"<>#\t :/"),
        kCFStringEncodingUTF8) autorelease];
}
@end

在这里它再次作为ARC符合内联函数帮助器:

#if __has_feature(objc_arc)
static inline NSString *hxURLEscape(NSString *v) {
    static CFStringRef _hxURLEscapeChars = CFSTR("?=,!$&'()*+;@?\r\n\"<>#\t :/");
    return ((__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
        NULL, 
        (__bridge CFStringRef)[v mutableCopy], 
        NULL, 
        _hxURLEscapeChars, 
        kCFStringEncodingUTF8));
}
#endif


更短更可怕:)
实际上,我认为我们根本不需要复制它,可以用`self`替换`[[self mutableCopy] autorelease]`.关于它的更多细节可以在`CFURLCreateStringByAddingPercentEscapes`的文档中找到http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFURLRef/Reference/reference.html

2> Rog..:

-stringByAddingPercentEscapesUsingEncoding:不执行完整的转义编码.您应该使用手动添加编码-replaceOccurrencesOfString:withString:

这是一个完整的列表(镜像Gamecat的列表),最初建议在https://devforums.apple.com/message/15674#15674.正如尼克指出的那样,这很昂贵,所以不要只考虑完整列表而不考虑你逃避的真正要求.

NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];   
[escaped replaceOccurrencesOfString:@"$" withString:@"%24" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];

此代码肯定会对您的URL进行哈希处理.


我相信正确的代码更像是NSMutableString*escaped = [NSMutableString stringWithString:[actionString ...]]; 和NSMakeRange(0,[转义长度])]而不是wholeString.
推荐阅读
mobiledu2402851377
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有