带有包含特殊字符的 url 的 NSURLRequest [英] NSURLRequest with url containing special characters

查看:62
本文介绍了带有包含特殊字符的 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: @"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 看起来是正确的:

Using the debugger the NSURL looks correct:

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 的解释方式.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆