1

我遇到了同样的问题: NSURL with special characters

但是尝试了他们的解决方案。无法让我的 NSURLRequest 使用 åöä 字符。如果变量“字符串”包含 åöä,则请求返回 null。还尝试了 NSISOLatin1StringEncoding。

NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: @"http://suggestqueries.google.com/complete/search?output=firefox&q=%@", encodedString];

NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

这有效: http ://suggestqueries.google.com/complete/search?output=firefox&q= %C3%A5%C3%B6%C3%A4 (åöä)

有任何想法吗?

编辑:使用调试器 NSURL 看起来是正确的:

string  __NSCFString *  @"åäö"  0x0a77cd60
encodedString   __NSCFString *  @"%C3%A5%C3%A4%C3%B6"   0x0a77fc40
url NSURL * @"http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%A4%C3%B6" 0x0a79d1f0

已解决:问题不在于 NSURL,而在于如何解释返回的 NSDATA。

4

1 回答 1

2

而不是使用

NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

尝试使用

NSString *encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString: encoded]];
于 2019-02-07T14:37:37.907 回答