问题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; }