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

2020-01-16  本文已影响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;
}
上一篇 下一篇

猜你喜欢

热点阅读