• ios url网址相关问题解说


    问题1:ios网址中存在汉字的情况,需要GB_18030_2000编码方法如下:

    // 汉字转编码
    + (NSString *)changeChineseWithEncodingGB_18030_2000:(NSString *)string{
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        NSData *data = [string dataUsingEncoding:enc];
        NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];
        return retStr;
    }

    问题2:H5中url 的encode/decode

    + (NSString *)URLDecodedString:(NSString *)str{
        NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
        
        return decodedString;
    }
    + (NSString *)URLEncodedString:(NSString *)str{
        NSString *encodedString = (NSString *)
        CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                  (CFStringRef)str,
                                                                  NULL,
                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                  kCFStringEncodingUTF8));
        
        return encodedString;
    }

    问题3:网址暴力解析参数

    + (NSDictionary *)analysisWebUrl:(NSString *)urlStr{
        NSRange range = [urlStr rangeOfString:@"?"];
        if (range.length == 0) {
            return @{};
        }
        NSString *propertys = [urlStr substringFromIndex:(int)(range.location+1)];
    
        NSArray *subArray = [propertys componentsSeparatedByString:@"&"];
        
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithCapacity:4];
        for (int j = 0 ; j < subArray.count; j++)
        {
            NSArray *dicArray = [subArray[j] componentsSeparatedByString:@"="];
            [tempDic setObject:dicArray[1] forKey:dicArray[0]];
        }
        return tempDic;
    }

    问题4:json转化, post中复杂格式数据下 需要转json

    //字典转json
    + (NSString*)dictToJson:(NSDictionary*)dict{
        NSLog(@"%@",dict);
        if (dict==nil) {
            return nil;
        }
        NSData*data=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
        NSString*string=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        return string;
    }
    //json转字典
    + (NSDictionary*)jsonToDict:(NSString*)string{
        if (string==nil) {
            return nil;
        }
        NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                             
                                                            options:NSJSONReadingMutableContainers
                             
                                                              error:&err];
        if(err) {
            NSLog(@"json解析失败:%@",err);
            return nil;
        }
        return dic;
    }
  • 相关阅读:
    echars 折线图使用
    echars 饼图使用
    React native中DrawerNavigator,StackNavigator,TabNavigator导航栏使用
    React native 中 SectionList用法
    React native 中使用Fetch请求数据
    React native中使用XMLHttpRequest请求数据
    实现在WebView中返回上一级
    DatePickerAndroid用法
    Lambda
    .Net常见线程安全问题整理
  • 原文地址:https://www.cnblogs.com/chaochaobuhuifei55/p/6164379.html
Copyright © 2020-2023  润新知