0
我在iPhone中使用MGtwitterengine,我想使用USER搜索API http://api.twitter.com/1/users/search.json?q={username},但在MGTwitterengine中找不到任何此方法。我如何在iphone中使用此API來獲取用戶。 感謝如何在MGTwitterengine中使用twitter用戶搜索api iphone
我在iPhone中使用MGtwitterengine,我想使用USER搜索API http://api.twitter.com/1/users/search.json?q={username},但在MGTwitterengine中找不到任何此方法。我如何在iphone中使用此API來獲取用戶。 感謝如何在MGTwitterengine中使用twitter用戶搜索api iphone
使用這樣: -
- (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 *[email protected]" ";
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);
}
謝謝iProgrammer。 – Leena
你找到任何解決方案? – Leena
是的,請參閱下面的答案 – iProgrammer