目标-C stringByAddingPercentEncodingWithAllowedCharacters无法正常工作

用户名

我有一个类似ANC和SHO.pdf的URL

当我像这样编码URL时:

NSString *escapedString = [PDFPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

现在,当我使用此URL时,它不起作用,并且我感觉它与&有关,因为当我尝试另一个文件时,它可以完美地工作,我能够加载PDF,而与&我的文件则无法。

我究竟做错了什么?

这是scapedString的输出

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;
}
Hong Duan

[NSCharacterSet URLHostAllowedCharacterSet] 包含以下字符:

!$&'()*+,-.0123456789:;=ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz~

其中包含“ &”,所以

NSString *escapedString = [PDFPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

不会为您转义'&',那么它不是URLEncoded。

请参阅有关如何对字符串进行url编码问题

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章