如何从CFURLCreateStringByAddingPercentEscapes迁移到stringByAddingPercentEncodingWIthAllowedCharacters?

时间:2017-07-04 15:59:41

标签: ios objective-c xcode deprecated

在我的代码中,从iOS 9开始,方法CFURLCreateStringByAddingPercentEscapes被标记为已弃用。我的代码如下:

static NSString *encodeByAddingPercentEscapes(NSString *input) {
    NSString *encodedValue = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)input, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
    return encodedValue;
}

警告提示我使用[NSString stringByAddingPercentEncodingWithAllowedCharacters:]

如何使用较新的方法完成相同的结果?

1 个答案:

答案 0 :(得分:0)

你可以使用[@"" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]。 并且有许多设置使用:URLPathAllowedCharacterSet,URLQueryAllowedCharacterSet.etc