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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8486435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:44:09  来源:igfitidea点击:

NSURL is null while NSString is correct in Objective-C

iphoneobjective-ciosxcodensstring

提问by Magnus

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

我有一个NSString包含 url 的,当我NSURLNSString, NSURL 输出(空)分配时。这是因为url中存在一些非法字符,NSURL如果不对NSString包含该url的内容进行编码就无法读取。

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 NSURLbeing (null)

IncomingUrlString 包含挪威字母“?”,我认为这是存在的原因NSURL(空)

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. 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 加载到UIWebView. 它适用于除此之外的所有其他传入 url,除了文件名外,它们看起来都一样。这是唯一包含非法字符的字符。但是我必须找到一种方法来对此进行编码,因为会有更多的文件包含“?”、“?” 或者 ”?” 在将来。

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://blahblah because of security reasons.

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

Can anyone help?

任何人都可以帮忙吗?

回答by omz

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:

您用于对字符串中的字符进行百分比编码的方法也会对合法的 URL 字符进行转义。如果您正在对 URL 参数进行编码,这将是合适的,但在这种情况下,最好简单地使用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

回答by V1ru8

If you have an URL that is a file path you must use + (id)fileURLWithPath:(NSString *)path. For the URLWithString:method the String must contain a scheme like file://or http://.

如果您的 URL 是文件路径,则必须使用+ (id)fileURLWithPath:(NSString *)path. 对于该URLWithString:方法,字符串必须包含类似file://或的方案http://

回答by C?ur

stringByAddingPercentEscapesUsingEncodingis 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.
The key part is to allow characters .and /and disallow %. CharacterSet.urlPathAllowedfits the requirements.

文件路径由https://tools.ietf.org/html/rfc8089定义。
关键部分是允许的字符./并禁止%CharacterSet.urlPathAllowed符合要求。

Output with your example:

输出与您的示例:

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

传入字符串
:/
url/path/fileName_bl?.pdf 编码字符串:/url/path/fileName_bl%C3%A5.pdf网址:/url/path/fileName_bl%C3%A5.pdf

回答by karim

I found also that for some North European characters, NSISOLatin1StringEncoding fits better.

我还发现对于一些北欧字符, NISOLatin1StringEncoding 更适合。

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