NSURL is null and NSString is in Objective-C

I have an NSString containing the url, and when I NSURL using NSString , the NSURL returns (null). This is because there are invalid characters in the URL that NSURL cannot read without NSString encoding containing the URL.

 NSString *u = [incomingUrlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:u]; NSLog(@"INCOMINGURLSTRING: %@" , u); NSLog(@"URL: %@" , url); 

Output:

  INCOMINGURLSTRING: /url/path/fileName_blå.pdf URL: (null) 

incomingUrlString contains the Norwegian letter "å", which, in my opinion, is the reason that NSURL is (null)

I also tried this:

 NSString *trimmedString = [file stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)trimmedString, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", kCFStringEncodingUTF8); NSLog(@"TRIMMEDSTRING: %@" , trimmedString); NSLog(@"ENCODEDSTRING: %@" , [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]); NSURL *url = [NSURL URLWithString:encodedString]; NSLog(@"URL: %@" , url); 

Here's the conclusion:

  TRIMMEDSTRING: /url/path/fileName_blå.pdf ENCODEDSTRING: /url/path/fileName_blå.pdf URL: %2Furl%2FPath%2FfileName_bl%C3%A5.pdf 

My goal is to load the url into a UIWebView . It works for all other incoming URLs except that they all look the same except for the file name. This is the only thing that is illegal. But I have to find a way to encode this, because in the future there will be more files containing either "æ", "ø" or "å".

I know that the result does not look correct according to url standards, which I did on purpose. I cannot show the correct URL with http: // blah blah for security reasons.

Can anyone help?

+4
source share
3 answers

The method that you use for percent encoding characters in a string also excludes legal URL characters. This would be appropriate if you encoded the URL parameter, in which case it would be better to just use stringByAddingPercentEscapesUsingEncoding: because it leaves characters that are part of the URL structure (':', '/', etc.). without changes:

 NSString *u = @"http://example/path/fileName_blå.pdf"; u = [u stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:u]; NSLog(@"%@", url); // http://example.com/path/fileName_bl%C3%A5.pdf 
+11
source

If you have a URL, file path, you should use + (id)fileURLWithPath:(NSString *)path . For the URLWithString: method URLWithString: string must contain a scheme of type file:// or http:// .

+2
source

I also found that for some Northern European characters, NSISOLatin1StringEncoding works better.

 - (void) testEncoding { NSString * urlString = @"http://example/path/fileName_blå.pdf"; urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]; NSURL * url = [NSURL URLWithString:urlString]; NSLog(@"URL: %@", url); } 
0
source

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


All Articles