【问题标题】:Unsupported URL iOS不支持的 URL iOS
【发布时间】:2015-03-31 19:09:09
【问题描述】:

我有一个有效的 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 的方式:

方法一:

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];

方法二:

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

【问题讨论】:

    标签: ios objective-c xcode http url


    【解决方案1】:

    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);
    

    输出:

    转义路径:http://fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065\

    参见 URL 编码字符集Documentation

    【讨论】:

    • 很酷的完美答案:)
    • 甜蜜!不错的答案
    • 很好的答案! (测试看看我是否也对此评论表示赞同)
    猜你喜欢
    • 2015-05-07
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 2013-01-17
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多