0

我在 iPhone 中使用 MGtwitterengine,我想使用 USER search API http://api.twitter.com/1/users/search.json?q={username}但我在 MGTwitterengine 中找不到任何方法。如何在 iphone 中使用此 API 来获取用户。谢谢

4

1 回答 1

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);
}
于 2012-09-18T06:05:42.947 回答