Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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
不支持的URL iOS_Ios_Objective C_Xcode_Http_Url - Fatal编程技术网

不支持的URL iOS

不支持的URL iOS,ios,objective-c,xcode,http,url,Ios,Objective C,Xcode,Http,Url,我有一个有效的url,我得到一个不支持的url错误。有人能告诉我为什么吗 如您所见,有http:// // http://fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065 Error description=Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0

我有一个有效的url,我得到一个不支持的url错误。有人能告诉我为什么吗

如您所见,有http://

// http://fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065

Error description=Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x78f97920 {NSUnderlyingError=0x79f78bd0 "unsupported URL", NSLocalizedDescription=unsupported URL}
这就是我尝试初始化url的方式:

方法1:

NSString *path=@"http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065";
NSURL *url=[NSURL URLWithString:path];
NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:url];
方法2:

NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065"]];

URL不能包含不在ASCII字符集中的字符,必须对这些字符进行转义

使用
stringByAddingPercentEncodingWithAllowedCharacters
与字符集
URLQueryAllowedCharacterSet

NSString *path = @"http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065";
NSString *escapedPath = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"escapedPath: %@", escapedPath);
输出:

escapedPath: http://fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065\ 转义路径:http://fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065\
请参阅URL编码的字符集

酷的完美答案:)甜美!回答得好回答得好!(测试看看我是否也能获得对该评论的支持票)