通过网上查找,参考了别人
url 为:
woaiwodejiaxiang://cn.jiaxiang.ZG/loader?dt=1254896584562&cityId=tr&deviceNumber=13185058252&userId=20985532&roleType=2&state=1&token=dad8fa595beda8306ea1aa01250aeb0&deviceIMEI=3951545154614522
1.
NSString *urlString = [NSString stringWithFormat:@"%@",url];// 将URL转化为NSString
NSMutableArray *urlArray =[NSMutableArray arrayWithObject:urlString];// 并放在数组中
NSRange range = [urlArray[0] rangeOfString:@"?"]; // 获取问号的位置 问号后面是参数列表。 urlArray[0] :我只有一个元素在数组中
NSString *propertys = [urlArray[0] substringFromIndex:(int)range.location]; 参数列表
NSArray *subArray =[propertys componentsSeparatedByString:@"&"]; // 进行字符串的拆分,通过&来拆分,
2.
NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithCapacity:8];// 8 是 我的URL中有8个参数 最总得到的字典有8个键值对
for (int j = 0; j<subArray.count; j++) {
NSArray *dicArray = [subArray[j] componentsSeparatedByString:@"="]; // 等号“=” 是用来分割键值对
[tempDic setObject:dicArray[1] forKey:dicArray[0]];// 给字典添加键值对关系,(我参考别人的)
}
NSLog(@"打印参数列表生成的字典: %@", tempDic);// 输出转化的字典
{
"?dt" = 1254896584562;
cityId = gy;
deviceIMEI = 3951545154614522;
deviceNumber = 13185058252;
roleType = 1;
state = 1;
token = dad8fa595beda8306ea1aa01250aeb0;
userId = 20985532;
}