Objective c NSURL:使用NSCharacterSet转义反斜杠

Objective c NSURL:使用NSCharacterSet转义反斜杠,objective-c,nsstring,ios9,nsurl,backslash,Objective C,Nsstring,Ios9,Nsurl,Backslash,这可能是一个重复的问题,但我已经检查了所有地方,找不到iOS9的有效答案-StringByAddingPercentescapesusingEncode已被弃用。我需要使用-stringByAddingPercentEncodingWithAllowedCharacters 下面是需要转义反斜杠的字符串,以便API能够验证会话并返回响应 NSString *base = @"http://domain.com/interface/?end=imember"; NSCharacterSet *se

这可能是一个重复的问题,但我已经检查了所有地方,找不到iOS9的有效答案-StringByAddingPercentescapesusingEncode已被弃用。我需要使用-stringByAddingPercentEncodingWithAllowedCharacters

下面是需要转义反斜杠的字符串,以便API能够验证会话并返回响应

NSString *base = @"http://domain.com/interface/?end=imember";
NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet];
NSString *key = [@"&client_key=KOB3N6KX9JXF2MRPO5U.BRFYM7TYVE\/16KIJVXZA6R7H\/1LD1K\/JYIYG7IZP2HA7NUYOVNT3CJG==&token=SGD7E9B29TQ.8HIITZ37XW3GLK5OGLZNLCDM=" stringByAddingPercentEncodingWithAllowedCharacters:set];
标准URL字符集不会转义反斜杠,我已经全部尝试过了:

URLUserAllowedCharacterSet
URLPasswordAllowedCharacterSet
URLHostAllowedCharacterSet
URLPathAllowedCharacterSet
URLQueryAllowedCharacterSet
URLFragmentAllowedCharacterSet
如果有人能帮助我,我对发展还比较陌生。是否可以创建包含反斜杠的自定义允许集

编辑:

这是url的外观:

http://domain.com/interface/?end=imember&client_key=KOB3N6KX9JXF2MRPO5U.BRFYM7TYVE\/16KIJVXZA6R7H\/1LD1K\/JYIYG7IZP2HA7NUYOVNT3CJG==&token=SGD7E9B29TQ.8HIITZ37XW3GLK5OGLZNLCDM=

你的答案的确切答案如下。我从中得到的。这是比其他答案更好的答案

NSString *unescaped = @"http://domain.com/interface/?end=imember"];
NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSLog(@"escapedString: %@", escapedString);
URL编码字符集是

URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
URLHostAllowedCharacterSet      "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet     "#%<>[\]^`{|}
URLUserAllowedCharacterSet      "#%/:<>?@[\]^`
URLFragmentAllowedCharacterSet“\\%[\]^`{124;}
URLHostAllowedCharacterSet“#%/?@^`{124;}
URLPasswordAllowedCharacterSet“#%/:?@[\]^`{124;}
URLPathAllowedCharacterSet“#%;?[\]^`{|}
URLQueryAllowedCharacterSet“\%[\]^`{124}
URLUserAllowedCharacterSet“#%/:?@[\]^`

当我使用URLHostAllowedCharacterSet时,结果是:
客户端密钥=KOB3N6KX9JXF2MRPO5U.BRFYYGM7TYVE%2fy16kijvxzhua6r7h%2F1LDD1K%2FJYIY22G7IZP2HA7NUYOVJWEHNT3CJG==&token=SGD7XXL。。。LZNLCDM=
它已将反斜杠替换为百分比符号:-)