开发中有时候在跳转网页的时候可能需要对后台返回的网址做一些处理。如提取某些参数或删除某些参数。如果直接判断字符串中是否包含该参数,但是参数的长度又不好控制。因此我实现了一个算法,通过对一个合法的url(第一个参数用"?"连接,且url中只有一个"?",剩下的参数用"&"连接)进行字符串分割等处理实现了url中参数的提取。
算法如下。
/**
获取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,@{}];
}
}
测试用例
NSString *str1 = @"https://www.jd.com/";
NSString *str2 = @"https://www.jd.com/?";
NSString *str3 = @"https://www.jd.com/?device=app";
NSString *str4 = @"https://www.jd.com/?device=app&appVersion=1.0.7&";
NSString *str5 = @"https://www.jd.com/?device=app&appVersion=1.0.7&platform=";
NSString *str6 = @"https://www.jd.com/?device=app&appVersion=&platform=iOS";
NSString *str7 = @"https://www.jd.com/?device=app&appVersion=1.0.7&platform=iOS";
NSLog(@"%@\n",[AppDelegate getParamsWithUrlString:str1]);
NSLog(@"%@\n",[AppDelegate getParamsWithUrlString:str2]);
NSLog(@"%@\n",[AppDelegate getParamsWithUrlString:str3]);
NSLog(@"%@\n",[AppDelegate getParamsWithUrlString:str4]);
NSLog(@"%@\n",[AppDelegate getParamsWithUrlString:str5]);
NSLog(@"%@\n",[AppDelegate getParamsWithUrlString:str6]);
NSLog(@"%@\n",[AppDelegate getParamsWithUrlString:str7]);
打印结果
2018-02-02 09:56:53.316819+0800 环形渐变测试[1572:274749] 链接不包含参数!
2018-02-02 09:56:53.316978+0800 环形渐变测试[1572:274749] (
"https://www.jd.com/",
{
}
)
2018-02-02 09:56:53.317134+0800 环形渐变测试[1572:274749] (
"https://www.jd.com/",
{
}
)
2018-02-02 09:56:53.317329+0800 环形渐变测试[1572:274749] (
"https://www.jd.com/",
{
device = app;
}
)
2018-02-02 09:56:53.317457+0800 环形渐变测试[1572:274749] (
"https://www.jd.com/",
{
appVersion = "1.0.7";
device = app;
}
)
2018-02-02 09:56:53.317586+0800 环形渐变测试[1572:274749] (
"https://www.jd.com/",
{
appVersion = "1.0.7";
device = app;
platform = "";
}
)
2018-02-02 09:56:53.317690+0800 环形渐变测试[1572:274749] (
"https://www.jd.com/",
{
appVersion = "";
device = app;
platform = iOS;
}
)
2018-02-02 09:56:53.317771+0800 环形渐变测试[1572:274749] (
"https://www.jd.com/",
{
appVersion = "1.0.7";
device = app;
platform = iOS;
}
)