简体   繁体   English

NSURL为null,而在Objective-C中NSString是正确的

[英]NSURL is null while NSString is correct in Objective-C

I have an NSString containing a url and when I allocate NSURL with the NSString , NSURL outputs (null). 我有一个包含url的NSString ,当我使用NSString分配NSURL时,NSURL输出(null)。 It's because there are some illegal characters in the url, which NSURL can't read without encoding the NSString containing the url. 这是因为url中存在一些非法字符,如果不对包含url的NSString进行编码,则NSURL无法读取。

NSString *u = [incomingUrlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:u];

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

Output is: 输出是:

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

incomingUrlString contains the Norwegian letter "å", which I think is the reason for the NSURL being (null) incomingUrlString包含挪威语字母“å”,我认为这是NSURL的原因(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 the output is: 这里的输出是:

 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 . 我的目标是将URL加载到UIWebView It works for all the other incoming urls except for this one, they all look the same except for the filename. 它适用于除此之外的所有其他传入URL,除文件名外,它们看起来都相同。 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. 但我必须找到一种方法来编码,因为将来会有更多的文件包含“æ”,“ø”或“å”。

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. 由于安全原因,我无法使用http:// blah blah显示正确的URL。

Can anyone help? 有人可以帮忙吗?

The method you're using for percent-encoding the characters in the string also escapes legal URL characters. 您用于对字符串中的字符进行百分比编码的方法也会转义合法的URL字符。 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: 如果你编码一个URL参数,这是合适的,在这种情况下,虽然最好简单地使用stringByAddingPercentEscapesUsingEncoding:因为它stringByAddingPercentEscapesUsingEncoding:了作为URL结构(':','/'等)一部分的字符完好无损:

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

If you have an URL that is a file path you must use + (id)fileURLWithPath:(NSString *)path . 如果您的URL是文件路径,则必须使用+ (id)fileURLWithPath:(NSString *)path For the URLWithString: method the String must contain a scheme like file:// or http:// . 对于URLWithString:方法,String必须包含类似file://http://

stringByAddingPercentEscapesUsingEncoding is deprecated. 不推荐使用stringByAddingPercentEscapesUsingEncoding

The new way (iOS 7+) to do it is: 新方法(iOS 7+)是这样做的:

NSString *encoded = [raw stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet];

File path is defined by https://tools.ietf.org/html/rfc8089 . 文件路径由https://tools.ietf.org/html/rfc8089定义。
The key part is to allow characters . 关键部分是允许角色. and / and disallow % . /和不允许% CharacterSet.urlPathAllowed fits the requirements. CharacterSet.urlPathAllowed符合要求。

Output with your example: 用你的例子输出:

incomingString: /url/path/fileName_blå.pdf incomingString:/url/path/fileName_blå.pdf
encodedString: /url/path/fileName_bl%C3%A5.pdf encodedString:/url/path/fileName_bl%C3%A5.pdf
URL: /url/path/fileName_bl%C3%A5.pdf URL:/url/path/fileName_bl%C3%A5.pdf

I found also that for some North European characters, NSISOLatin1StringEncoding fits better. 我还发现,对于一些北欧角色,NSISOLatin1StringEncoding更适合。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM