NSURL为null,而NSString在Objective-C中正确 [英] NSURL is null while NSString is correct in Objective-C

查看:81
本文介绍了NSURL为null,而NSString在Objective-C中正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含URL的NSString,当我用NSString分配NSURL时,NSURL输出(空).这是因为url中有一些非法字符,如果不对包含URL的NSString进行编码,则NSURL无法读取.

I have an NSString containing a url and when I allocate NSURL with the NSString, NSURL outputs (null). It's because there are some illegal characters in the url, which NSURL can't read without encoding the NSString containing the url.

NSString *u = [incomingUrlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:u];

NSLog(@"INCOMINGURLSTRING: %@" , u);
NSLog(@"URL: %@" , url);

输出为:

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

incomingUrlString包含挪威字母å",我认为这是NSURL为(空)的原因

incomingUrlString contains the Norwegian letter "å", which I think is the reason for the NSURL being (null)

我也尝试过:

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);

这里的输出是:

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

我的目标是将URL加载到UIWebView中.它适用于除此URL以外的所有其他传入URL,除了文件名外,它们看起来都相同.这是唯一包含非法字符的人.但是我必须找到一种编码方法,因为将来会出现更多包含æ",ø"或å"的文件.

My goal is to load the URL into a UIWebView. It works for all the other incoming urls except for this one, they all look the same except for the filename. This is the only one containg an illegal character. But I have to find a way to encode this, because there will be more files containg either "æ", "ø" or "å" in the future.

我知道根据url标准,输出看起来不正确,这是我故意这样做的.出于安全原因,我无法使用 http://blah blah显示正确的网址.

I know the output does not look correct according to url standards, which I did on purpose. I can't show the correct url with http://blah blah because of security reasons.

任何人都可以帮忙吗?

推荐答案

您用于对字符串中的字符进行百分比编码的方法也会转义合法的URL字符.如果您要编码URL参数,这将是适当的,在这种情况下,尽管最好使用stringByAddingPercentEscapesUsingEncoding:,因为它会保留URL结构中的字符(':','/'等),这会更好.完整:

The method you're using for percent-encoding the characters in the string also escapes legal URL characters. This would be appropriate if you were encoding a URL parameter, in this case though it would be better to simply use stringByAddingPercentEscapesUsingEncoding: because it leaves the characters that are part of the URL's structure (':', '/', etc.) intact:

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

这篇关于NSURL为null,而NSString在Objective-C中正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆