如何从响应字符串中提取令牌? (我不知道令牌的长度,所以我不能在这里使用NSRange)
oauth_callback_confirmed=true&oauth_token=72157632316931441
-fadcd6ef70cbd06c&oauth_token_secret=a7e7b046a8960559
当前代码是(它给出了令牌和字符串的其余部分):
NSRange access_token_range = [operation.responseString rangeOfString:@"oauth_token="];
if (access_token_range.length > 0) {
int from_index = access_token_range.location + access_token_range.length;
NSString *access_token = [operation.responseString substringFromIndex:from_index];
NSLog(@"access_token: %@", access_token);
}
答案 0 :(得分:1)
将响应字符串分隔为键值对然后单独处理它们会更好(更优雅):
NSString *token = nil;
NSArray *kvpairs = [operation.responseString componentsSeparatedByString:@"&"];
for (NSString *kvpair in kvpairs) {
NSArray *keyAndValue = [kvpair componentsSeparatedByString:@"="];
NSString *key = [keyAndValue objectAtIndex:0];
if ([key isEqualToString:@"oauth_token"]) {
token = [keyAndValue objectAtIndex:1];
break;
}
}
现在token
将包含令牌,如果无法找到,则会nil
。