Objective - C stringByAddingPercentEncodingWithAllowedCharacters not working

I have a URL similar to ANC and SHO.pdf

when I encode the url like this:

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

now when i use this url it doesn't work and i have a feeling that it is related to that and because when i tried another file it worked fine i was able to download the pdf file but with the file with and I was not.

What am I doing wrong?

Here is the result of escapedString

escapedString   __NSCFString *  @"Ancaster%5CANC%20&%20SHO%20-%20Laundry%20Closets%20to%20be%20Checked.pdf" 0x16ea33c0

Then I use this to call the method:

NSArray *byteArray = [dataSource.areaData GetPDFFileData:[NSString stringWithFormat:@"%@",escapedString]];

Here is a way:

-(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;
}
+4
source share
1 answer

[NSCharacterSet URLHostAllowedCharacterSet] contains the following characters:

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

which contains ' &', therefore

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

does not run off '&' for you, then it is not URLEncoded.

. question , .

+6

Source: https://habr.com/ru/post/1609308/


All Articles