当前位置: 代码迷 >> 综合 >> 关于NSURL莫名其妙为空的问题,网页打不开
  详细解决方案

关于NSURL莫名其妙为空的问题,网页打不开

热度:16   发布时间:2023-10-26 13:54:44.0

链接中包换中文,会导致创建NSURL对象的时候,为nil。 下面是兼容代码

//兼容一下 链接中含有中文
-(NSURL *)cz_URLWithString:(NSString *)stringURL{if (stringURL.length ==0) {return nil;}NSURL *url = [NSURL URLWithString:stringURL];if (!url) {NSString *tmpStringURL = [stringURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];url = [NSURL URLWithString:tmpStringURL];}return url;
}