Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:如何进行正确的URL编码?_Ios_Url Encoding - Fatal编程技术网

iOS:如何进行正确的URL编码?

iOS:如何进行正确的URL编码?,ios,url-encoding,Ios,Url Encoding,我无法在UIWebView中打开URL,因此我搜索并发现我需要对URL进行编码,所以我尝试对其进行编码,但在URL编码中遇到了问题:我的URL是http://somedomain.com/data/Témp%20Page%20-%20Open.html(它不是真正的URL) 我关心的是%20,我试图用stringbyreplacingoccurrenceofstring:@“替换为string:@”,它给了我想要的URL,比如http://somedomain.com/data/Témp Pag

我无法在
UIWebView
中打开URL,因此我搜索并发现我需要对URL进行编码,所以我尝试对其进行编码,但在URL编码中遇到了问题:我的URL是
http://somedomain.com/data/Témp%20Page%20-%20Open.html
(它不是真正的URL)

我关心的是
%20
,我试图用
stringbyreplacingoccurrenceofstring:@“替换为string:@”
,它给了我想要的URL,比如
http://somedomain.com/data/Témp Page-Open.html
但是它并没有在
UIWebView
中打开,而是在
Safari
FireFox
完美中打开。即使我打开未编码的URL,它也会自动转换并打开我正在寻找的页面

我在谷歌上搜索过URL编码&它指向我已经检查过的不同结果,但没有结果帮我解决!!我尝试了不同的函数来回答不同的URL编码问题,但它只是改变了所有特殊字符,使我的URL类似于,
http%3A%2F%2Fsomedomain.com%2Fdata%2FT…
,它无法在
UIWebView
甚至任何浏览器中打开

它在
UIWebView委托中提供以下
错误日志

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { }
错误代码:101 &描述:Error Domain=webkiterrodomain Code=101“操作无法完成。(webkiterrodomain Error 101.)”UserInfo=0x6e4cf60{}


在iPhone中对URL进行编码非常简单。具体如下:

NSString* strURL = @"http://somedomain.com/data/Témp Page - Open.html";

NSURL* url = [NSURL URLWithString:[strURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
这是一个完美的方式来编码的网址,我正在使用它,它的完美工作与我


希望它能帮助你

你能试试这个吗

    //yourURL contains your Encoded URL
    yourURL = [yourURL stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    yourURL = [yourURL stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSLog(@"Keyword:%@ is this",yourURL);
我不确定,但在我的案例中,我已经解决了这个问题。
希望这能解决你的问题。

Dhaval Vaishnani提供的答案仅部分正确。此方法将
=
&
字符视为不进行编码,因为它们在URL中有效。因此,要对任意字符串进行编码以安全地用作URL的一部分,您不能使用此方法。相反,您必须退回到使用CoreFoundation和
CFURLRef

NSString *unsafeString = @"this &string= confuses ? the InTeRwEbZ";
CFStringRef safeString = CFURLCreateStringByAddingPercentEscapes (
    NULL,
    (CFStringRef)unsafeString,
    NULL,
    CFSTR("/%&=?$#+-~@<>|\\*,.()[]{}^!"),
    kCFStringEncodingUTF8
);

同样,不要忘记正确的内存管理。

您可能需要将URL分解为它的组成部分,然后URL编码主机和路径,而不是方案。然后把它重新组装起来


使用字符串创建NSURL,然后使用其上的方法(如主机、方案、路径、查询等)将其分离。然后使用CFURLCreateStringByAddingPercentEscapes对这些部分进行编码,然后您可以将它们重新组合到一个新的NSURL中。

我做了一些测试,我认为问题并不在于
UIWebView
而是
NSURL
不会接受URL,因为“témp”中的é没有正确编码。这将导致
+[nsurlRequestRequestWithURL:
-[nsurlUrlWithString:
返回
nil
,因为字符串包含格式错误的URL。我猜您最终会使用
nil
请求和
-[UIViewWeb loadRequest::
,这是不好的

例如:

NSLog(@"URL with é: %@", [NSURL URLWithString:@"http://host/Témp"]);
NSLog(@"URL with encoded é: %@", [NSURL URLWithString:@"http://host/T%C3%A9mp"]);
输出:

2012-10-02 12:02:56.366 test[73164:c07] URL with é: (null)
2012-10-02 12:02:56.368 test[73164:c07] URL with encoded é: http://host/T%C3%A9mp
如果您真的想借用WebKit对格式错误URL的优雅处理,而不想自己实现,您可以这样做,但这非常难看:

UIWebView *webView = [[[UIWebView alloc]
                       initWithFrame:self.view.frame]
                      autorelease];

NSString *url = @"http://www.httpdump.com/texis/browserinfo/Témp.html";

[webView loadHTMLString:[NSString stringWithFormat:
                         @"<script>window.location=%@;</script>",
                         [[[NSString alloc]
                           initWithData:[NSJSONSerialization
                                         dataWithJSONObject:url
                                         options:NSJSONReadingAllowFragments
                                         error:NULL]
                           encoding:NSUTF8StringEncoding]
                          autorelease]]
                baseURL:nil];
UIWebView*webView=[[[UIWebView alloc]
initWithFrame:self.view.frame]
自动释放];
NSString*url=@”http://www.httpdump.com/texis/browserinfo/Témp.html“;
[webView loadHTMLString:[NSString stringWithFormat:
@“window.location=%@;”,
[[NSString alloc]
initWithData:[NSJSONSerialization
dataWithJSONObject:url
选项:NSJSONReadingAllowFragments
错误:NULL]
编码:NSUTF8StringEncoding]
自动释放]]
baseURL:nil];

这可能对研究URL编码问题的人很有用,因为我的问题可能与已经解决和接受的问题不同,这是我过去进行编码的方式

-(NSString *)encodeURL:(NSString *)urlString
{
    CFStringRef newString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)urlString, NULL, CFSTR("!*'();:@&=+@,/?#[]"), kCFStringEncodingUTF8);
    return (NSString *)CFBridgingRelease(newString);
}

最直接的方法是使用:

NSString *encodedString = [rawString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
iDhaval很接近,但他正以另一种方式进行(解码而不是编码)

阿南德的方法可行,但你很可能需要替换更多的字符,而不是空格和新行。请参阅此处的参考资料:

希望有帮助。

你可以试试这个

NSString *url = @"http://www.abc.com/param=Hi how are you";

NSString* encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:
 NSASCIIStringEncoding];

我想这对你有用

[strUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]

URL编码的本机方法。

Swift 4.x

let originalString = "https://www.somedomain.com/folder/some cool file.jpg"
let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print(escapedString!)

达瓦尔,谢谢你的快速回复!我以前也尝试过同样的方法,但是没有结果,但是我也尝试过你的代码,当我
NSLog
url时,它会给出
(null)
。url是
http://somedomain.com/data/Témp%20Page%20-%20Open.html
请注意,
+[NSString stringbyreplacingpercentescapesusingencode:][/code>的行为有点奇怪,请参阅这篇博文@Dhaval Vaishnani。另外,您使用的邮件发送不会对未转义的URL字符串进行编码,而是解码已转义百分比的URL字符串。这将替换转义百分比序列,而不是创建它们。对URL进行百分比编码的过程是用
%3a
格式的百分比编码替换保留字符,例如,此方法用相应的保留字符替换保留字符。正如@user529758所具有的,这是一个解码函数。URL看起来已经是URL编码的格式了,除了é-字符,它可能应该编码为%c3%a9。为什么它能在Safari等中工作,可能是因为桌面浏览器对无效的URL非常开放,并且会尝试为您修复它们,即假定é应该是URL编码的。@MattiasWadman,如果将相同的URL粘贴到浏览器中,它将使用
空格
转换
%20
,而不是编码
é
,所以
let originalString = "https://www.somedomain.com/folder/some cool file.jpg"
let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print(escapedString!)