/** 对字典(Key-Value)排序
@param dict 要排序的字典
*/
+ (NSMutableDictionary *)sortedDictionary:(NSMutableDictionary *)dict {
NSMutableString *contentString =[NSMutableString string];
NSArray *keys = [dict allKeys];
//排序
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
//拼接 把排序后的字典拼接成string
for (NSString *keyStr in sortedArray) {
if (![[dict objectForKey:keyStr] isEqualToString:@""] && ![[dict objectForKey:keyStr] isEqualToString:@"key"] ) {
[contentString appendFormat:@"%@=%@&", keyStr, [dict objectForKey:keyStr]];
}
}
contentString = (NSMutableString *)[contentString substringToIndex:contentString.length-1];
NSLog(@"%@", contentString);
//加密 把拼接后的参数加密生产sign
NSString *signStr = [[NSString stringWithFormat:@"%@%@", contentString, MX_KEY] MD5];
[dict setObject:signStr forKey:@"sign"];
return dict;
}
网友评论