iOS中经常遇到的url 问题导致白屏,大部门人直接对整个URL 进行转译如
[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
这样是可以解决url中的中文问题,但是 url 如果是以下https://ts.keytop.cn/tcyfront/#/Apps?showBottom=0 这种类型的 其中的#号也会被转译成%23导致前端页面不能通过#号来做路由跳转。
我个人的处理方法是
-(NSString*)URLEncodedString:(NSString*)url{
//先处理空格
url = [urlstringByReplacingOccurrencesOfString:@" " withString:@""];
//判断是否有参数
NSArray *array = [url componentsSeparatedByString:@"?"];
if(array.count>=2) {
NSMutableString* nurl = [NSMutableStringstringWithFormat:@"%@",array[0]];
for(inti=1; i
NSString *parm = [array[i] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[nurlappendFormat:@"?%@",parm];
}
return nurl;
}
else{
//判断是否有#路由符号
array = [urlcomponentsSeparatedByString:@"/#/"];
if(array.count==2)
{
NSString *parm = [array[1] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
return [NSStringstringWithFormat:@"%@?%@",array[0],parm];
}
}
return [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];;
}
网友评论