2012-06-28 92 views

回答

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 *[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); 
} 
+0

謝謝iProgrammer。 – Leena

相關問題