本文介绍了带有包含特殊字符的 url 的 NSURLRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了同样的问题:带有特殊字符的 NSURL
但是尝试了他们的解决方案.无法让我的 NSURLRequest 使用 åöä 字符.如果变量string"包含 åöä,则请求返回 null.还尝试使用 NISOLatin1StringEncoding.
But tried their solution. Can't get my NSURLRequest to work with åöä characters. If the variable "string" contains åöä the request return null. Also tried with NSISOLatin1StringEncoding.
NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *urlString = [NSString stringWithFormat: @"suggestqueries.google/complete/search?output=firefox&q=%@", encodedString]; NSURL *url = [NSURL URLWithString: urlString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];这有效:suggestqueries.google/complete/search?output=firefox&q=%C3%A5%C3%B6%C3%A4 (åöä)
有什么想法吗?
使用调试器,NSURL 看起来是正确的:
Using the debugger the NSURL looks correct:
string __NSCFString * @"åäö" 0x0a77cd60 encodedString __NSCFString * @"%C3%A5%C3%A4%C3%B6" 0x0a77fc40 url NSURL * @"suggestqueries.google/complete/search?output=firefox&q=%C3%A5%C3%A4%C3%B6" 0x0a79d1f0已解决:问题不是 NSURL,而是返回 NSDATA 的解释方式.
Solved: The problem was not the NSURL it was how the return NSDATA was interpreted.
推荐答案代替使用
NSURL *url = [NSURL URLWithString: urlString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];尝试使用
NSString *encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]]; NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString: encoded]];更多推荐
带有包含特殊字符的 url 的 NSURLRequest
发布评论