URL编码与解码那些事儿

    //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编码与解码那些事儿)