我在iPhone中使用MGtwitterengine,我想使用USER搜索API http://api.twitter.com/1/users/search.json?q={username},但我在MGTwitterengine中找不到任何方法。如何在iphone中使用此API来获取用户。 谢谢
答案 0 :(得分:1)
像这样使用: -
- (void)searchforTwUser {
OAToken *access_token = [[OAToken alloc] initWithKey:[tEngine oauthKey] secret:[tEngine oauthSecret]];
OAConsumer *aconsumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey
secret:kOAuthConsumerSecret];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
NSString *spaceString=@" ";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:self.searchName] invertedSet];
if ([spaceString rangeOfCharacterFromSet:set].location == NSNotFound)
{
NSString *Name = [self.searchName stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/users/search.json?q=%@",Name]];
NSLog(@"search name 1 is ..................................... %@",url);
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:aconsumer token:access_token realm:nil
signatureProvider:nil];
[request setHTTPMethod:@"GET"];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(searchTicket:didFinishWithData:)
didFailSelector:@selector(searchTicket:didFailWithError:)];
[request release];
}
else
{
NSString *addStr = @"%20";
NSString *firstCapChar = [[searchName substringToIndex:1] capitalizedString];
NSString *cappedString = [searchName stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/users/search.json?q=%@%@",cappedString,addStr]];
NSLog(@"search name 2 is ..................................... %@",url);
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:aconsumer token:access_token realm:nil
signatureProvider:nil];
[request setHTTPMethod:@"GET"];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(searchTicket:didFinishWithData:)
didFailSelector:@selector(searchTicket:didFailWithError:)];
[request release];
}
[access_token release];
[aconsumer release];
}
- (void) searchTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dict = [response objectFromJSONString];
NSLog(@"Dict %@",dict);
[twSearchArray removeAllObjects];
if (twSearchArray != nil) {
[twSearchArray release];
twSearchArray = nil;
}
twSearchArray = (NSMutableArray *)dict;
NSLog(@"Twitter %@",twSearchArray);
self.twLoaded = YES;
[twSearchArray retain];
[self prepareSearchResults];
[response release];
}
- (void) searchTicket:(OAServiceTicket *)ticket didFailWithError:(NSData *)error {
NSLog(@"Errors is %@",error.description);
}