/**
获取url中的参数并返回
@param urlString 带参数的url
@return @[NSString:无参数url, NSDictionary:参数字典]
*/
+ (NSArray*)getParamsWithUrlString:(NSString*)urlString {
if(urlString.length==0) {
NSLog(@"链接为空!");
return@[@"",@{}];
}
//先截取问号
NSArray*allElements = [urlStringcomponentsSeparatedByString:@"?"];
NSMutableDictionary *params = [NSMutableDictionary dictionary];//待set的参数字典
if(allElements.count==2) {
//有参数或者?后面为空
NSString*myUrlString = allElements[0];
NSString*paramsString = allElements[1];
//获取参数对
NSArray*paramsArray = [paramsStringcomponentsSeparatedByString:@"&"];
if(paramsArray.count>=2) {
for(NSIntegeri =0; i < paramsArray.count; i++) {
NSString*singleParamString = paramsArray[i];
NSArray*singleParamSet = [singleParamStringcomponentsSeparatedByString:@"="];
if(singleParamSet.count==2) {
NSString*key = singleParamSet[0];
NSString*value = singleParamSet[1];
if(key.length>0|| value.length>0) {
[paramssetObject:value.length>0?value:@""forKey:key.length>0?key:@""];
}
}
}
}elseif(paramsArray.count==1) {
//无 &。url只有?后一个参数
NSString*singleParamString = paramsArray[0];
NSArray*singleParamSet = [singleParamStringcomponentsSeparatedByString:@"="];
if(singleParamSet.count==2) {
NSString*key = singleParamSet[0];
NSString*value = singleParamSet[1];
if(key.length>0|| value.length>0) {
[paramssetObject:value.length>0?value:@""forKey:key.length>0?key:@""];
}
}else{
//问号后面啥也没有 xxxx? 无需处理
}
}
//整合url及参数
return@[myUrlString,params];
}elseif(allElements.count>2) {
NSLog(@"链接不合法!链接包含多个\"?\"");
return@[@"",@{}];
}else{
NSLog(@"链接不包含参数!");
return@[urlString,@{}];
}
}