美文网首页
URL编码与解码那些事儿

URL编码与解码那些事儿

作者: winsonGali | 来源:发表于2018-03-14 16:38 被阅读0次
        //URL编码
        NSString *urlString = @"https://www.winsonGali.com/我是中文啊";
        //iOS9之前
        NSString *encodeString8 = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        //iOS9之后
        NSString *encodeString9 = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        //URL解码
        NSString *encodeString = @"https://www.winsonGali.com/%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87%E5%95%8A";
        //iOS9之前
        NSString *hString8 = [encodeString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        //iOS9之后
        NSString *hString9 = [encodeString stringByRemovingPercentEncoding];
        
        NSLog(@"encodeString8 = %@", encodeString8);
        NSLog(@"encodeString9 = %@", encodeString9);
        NSLog(@"hString8 = %@", hString8);
        NSLog(@"hString9 = %@", hString9);
    

    打印结果是:

    encodeString8 = https://www.baidu.com/%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87%E5%95%8A
    encodeString9 = https://www.baidu.com/%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87%E5%95%8A
    hString8 = https://www.baidu.com/我是中文啊
    hString9 = https://www.baidu.com/我是中文啊
    

    相关文章

      网友评论

          本文标题:URL编码与解码那些事儿

          本文链接:https://www.haomeiwen.com/subject/kpnfqftx.html