Question

I am using MGtwitterengine in iPhone , I want to use USER search API http://api.twitter.com/1/users/search.json?q={username} but I don't find any method for this in MGTwitterengine. how can I use this API in iphone to get users. Thanks

Was it helpful?

Solution

Use like This :-

- (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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top