iOS - 获取URL中的参数

//name: url中的参数名称

-(NSString*)getParamByName:(NSString*)name URLString:(NSString*)url{NSError*error;NSString*regTags=[[NSString alloc]initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",name];NSRegularExpression*regex=[NSRegularExpression regularExpressionWithPattern:regTags options:NSRegularExpressionCaseInsensitive error:&error];// 执行匹配的过程NSArray*matches=[regex matchesInString:url options:0range:NSMakeRange(0,[url length])];for(NSTextCheckingResult*matchinmatches){NSString*tagValue=[url substringWithRange:[match rangeAtIndex:2]];// 分组2所对应的串returntagValue;}return@"";}


NSString *goodId = [self getParamByName:@"id" URLString:url];

goodId 取出来就是123456.

你可能感兴趣的:(iOS - 获取URL中的参数)