我遇到了同样的问题: NSURL with special characters
但尝试了他们的解决方案。无法让我的NSURLRequest与åöä字符一起使用。如果变量“string”包含åöä,则请求返回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的。
答案 0 :(得分: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]];