iOS 字符串含有特殊字符,转换URL为空值
第一种方法
url含有中文的解决方法
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)urlStr,NULL,NULL,kCFStringEncodingUTF8));
urlStr是url地址,拼接中含有中文。
第二种方法
解码:[@"编码后的字符串" stringByRemovingPercentEncoding];//解码
NSURL 转换字符串可以直接使用NSURL的属性absoluteString
例如上述的url,NSString * urlStr = url.absoluteString;
使用上面的方法无效,解决方法使用这个
NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
NSString * stringUrl = [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
//urlEncode解码
NSMutableString *outputStr = [NSMutableString stringWithString:input];
[outputStr replaceOccurrencesOfString:@"+" withString:@"" options:NSLiteralSearch range:NSMakeRange(0,[outputStr length])];
[outputStr stringByRemovingPercentEncoding];
参考:https://www.jianshu.com/p/b7dde2b1e992
http://blog.csdn.net/olive1993/article/details/52036755
https://www.tpyyes.com/a/kuozhan/2017/0406/89.html