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?