解决iOS开发中不合法的网络请求地址

  NSString *const kWebsite = @"http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fr=&sf=1&fmq=1459502303089_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=风景";

这是一个百度图片风景分类里的图片地址,当我发送请求时发生Unsuppotedurl错误。

 

百度了一下,产生错误的原因是URL 不能包含 ASCII 字符集,汉字,这样的字符必须进行转义的字符。

    NSString *urlString = [kWebsite stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

原文地址:https://www.cnblogs.com/fengmin/p/5848046.html