Ios 目标-C StringByAddingPercentEncoding与允许的字符不工作
我有一个类似于so ANC&SHO.pdf的URL 当我这样编码URL时:Ios 目标-C StringByAddingPercentEncoding与允许的字符不工作,ios,objective-c,Ios,Objective C,我有一个类似于so ANC&SHO.pdf的URL 当我这样编码URL时: NSString *escapedString = [PDFPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; 现在,当我使用这个URL时,它不起作用,我有一种感觉,它与&有关,因为当我尝试另一个文件时,它工作得非常好,我能够加载PDF,但对于带有的文件&我没有 我做错
NSString *escapedString = [PDFPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
现在,当我使用这个URL时,它不起作用,我有一种感觉,它与&有关,因为当我尝试另一个文件时,它工作得非常好,我能够加载PDF,但对于带有的文件&我没有
我做错了什么
这是escapedString的输出
escapedString __NSCFString * @"Ancaster%5CANC%20&%20SHO%20-%20Laundry%20Closets%20to%20be%20Checked.pdf" 0x16ea33c0
然后我用它来调用一个方法:
NSArray *byteArray = [dataSource.areaData GetPDFFileData:[NSString stringWithFormat:@"%@",escapedString]];
方法如下:
-(NSArray *)GetPDFFileData:(NSString *)PDFFile
{
NSString *FileBrowserRequestString = [NSString stringWithFormat:@"%@?PDFFile=%@",kIP,PDFFile];
NSURL *JSONURL = [NSURL URLWithString:FileBrowserRequestString];
NSURLResponse* response = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
if(data == nil)
return nil;
NSError *myError;
NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
return tableArray;
}
[NSCharacterSet URLHostAllowedCharacterSet]
包含以下字符:
!$&'()*+,-.0123456789:;=ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz~
其中包含“&
”,因此
NSString *escapedString = [PDFPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
不会为您逃过“&”,那么它就不是URL编码的
请参阅关于如何对字符串进行url编码。显示escapedString的内容。escapedString\uuu NSCFString*@“Ancaster%5CANC%20&%20SHO%20-%20洗衣房%20衣橱%20至%20be%20已检查。pdf”0x16ea33c0看起来正常。您是如何使用字符串获取文件的?可能重复您使用的是不同的方法,您需要在字符串中添加转义字符,请使用
[@“您的URL字符串”stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]