iOS进阶指南iOS开发技术分享iOS Developer

将URL中的一串参数,转成字典

2016-07-04  本文已影响542人  huanghy
-(void)viewDidLoad
{
    NSString *urlstr = @"http://test.m.lanbalanma.com/act/share/pyqOpen?t=&url=http://test.m.lanbalanma.com&pic=http%3A%2F%2Fimg.lanbalanma.com%2Fproduct%2F201509%2FD514B794-B619-4A1C-AF19-3AEC1222A46D_w507_h507.jpg&title=adsghjfhjh";
    NSRange start1 = [urlstr rangeOfString:@"pyqOpen?"];
    NSString *pyqOpenUrl = [urlstr substringWithRange:NSMakeRange(start1.location + start1.length, urlstr.length -start1.location - start1.length)];
    NSLog(@"pyqOpenUrl%@",pyqOpenUrl);
    NSString *pyqOpenUrl1 = [pyqOpenUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//有时候获取的url中的中文等字符是乱码,网页内容是乱码,需要进行一下转码才能正确识别NSString
    [self getGiftComStrWithStr:pyqOpenUrl1];
}

- (void)getGiftComStrWithStr:(NSString *)urlstr
{
    NSArray *array = [urlstr componentsSeparatedByString:@"&"];

    if (_array1.count > 0) {
        [_array1 removeAllObjects];
    }
    for (NSString *str in array) {
        NSString *str1 = [str  stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [_array1 addObject:str1];
    }
     NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];
     for (int j=0; j<_array1.count; j++) {
         NSArray *dicArray = [_array1[j] componentsSeparatedByString:@"="];
        [tempDict setObject:dicArray[1] forKey:dicArray[0]];
     }
     NSString *uid = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
     _url = [tempDict[@"url"] stringByAppendingFormat:@"=%@",uid];;
     _imageUrl = tempDict[@"pic"];  
     _title = tempDict[@"title"];
}
上一篇下一篇

猜你喜欢

热点阅读