如何在iPhone应用程序中检索Twitter用户名?

时间:2011-11-23 05:53:26

标签: iphone twitter

我们如何检索iPhone用户的Twitter用户名?我可以获得Facebook用户名。当用户登录Facebook时,以下代码为我提供了整个配置文件。我将此响应拆分并获取用户名,即名字和姓氏。

- (void)getFacebookProfile {
    NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@", [_accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURL *url = [NSURL URLWithString:urlString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDidFinishSelector:@selector(getFacebookProfileFinished:)];

    [request setDelegate:self];
    [request startAsynchronous];
}

#pragma mark FB Responses

- (void)getFacebookProfileFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    NSLog(@"Got Facebook Profile: %@", responseString);

    NSMutableDictionary *responseJSON = [responseString JSONValue];   

    NSString *username;
    NSString *firstName = [responseJSON objectForKey:@"first_name"];
    NSString *lastName = [responseJSON objectForKey:@"last_name"];
    if (firstName && lastName) {
        username = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
    } else {
        username = @"mysterious user";
    }
//[[NSUserDefaults standardUserDefaults] setObject:@"username" forKey:@"FB_LOGIN_STATUS"];
[_loginButton setTitle:username forState:UIControlStateNormal];

    [self refresh];    
}

#pragma mark FBFunLoginDialogDelegate

- (void)accessTokenFound:(NSString *)accessToken {
    NSLog(@"Access token found: %@", accessToken);
    self.accessToken = accessToken;

    _loginState = LoginStateLoggedIn;
    [self dismissModalViewControllerAnimated:YES];
    [self getFacebookProfile]; 
    [self refresh];
}

如何为Twitter实现此功能?

1 个答案:

答案 0 :(得分:0)

#pragma mark SA_OAuthTwitterControllerDelegate
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
    NSLog(@"Authenicated for %@", username);
    [_btnTwitter setTitle:username forState:UIControlStateNormal];
}

这是从twitter.Thanks重新获取用户名的代码。