+ (NSDictionary *)getURLParameters:(NSString *)strUrl {
// 查找参数
NSRange range = [strUrl rangeOfString:@"?"];
if (range.location == NSNotFound) {
return nil;
}
NSMutableDictionary *params = [NSMutableDictionary dictionary];
// 截取参数
NSString *parametersString = [strUrl substringFromIndex:range.location + 1];
// 判断参数是单个参数还是多个参数
if ([parametersString rangeOfString:@"&"].length) {
// 多个参数,分割参数
NSArray *urlComponents = [parametersString componentsSeparatedByString:@"&"];
for (NSString *keyValuePair in urlComponents) {
// 生成Key/Value
NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding];
// Key不能为nil
if (key == nil || value == nil) {
continue;
}
// 设置值
[params setValue:value forKey:key];
}
} else {
// 单个参数
// 生成Key/Value
NSArray *pairComponents = [parametersString componentsSeparatedByString:@"="];
// 只有一个参数,没有值
if (pairComponents.count == 1) {
return nil;
}
// 分隔值
NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding];
// Key不能为nil
if (key == nil || value == nil) {
return nil;
}
// 设置值
[params setValue:value forKey:key];
}
NSDictionary *parDic = [NSDictionary dictionaryWithDictionary:params];
return parDic;
}