如何在iOS 5中使用Twitter框架获取用户的Twitter个人资料信息?

Rah*_*air 3 twitter objective-c ios ios5

我可以使用以下代码发布到Twitter:

TWTweetComposeViewController *tweeter = [[TWTweetComposeViewController alloc] init];
        [tweeter setInitialText:@"message"];
        [tweeter addImage:image];
        [self presentModalViewController:tweeter animated:YES];
Run Code Online (Sandbox Code Playgroud)

如何在iOS 5中使用Twitter框架获取用户的Twitter个人资料信息?

Dan*_*rpe 6

好吧,我们说你想在桌面上显示用户在他们设备上的推特账号.您可能希望在表格单元格中显示头像,在这种情况下,您需要查询Twitter的API.

假设你有一个NSArrayACAccount对象,你可以创建一个字典来存储每个帐户额外的配置文件信息.你的表视图控制器tableView:cellForRowAtIndexPath:需要一些像这样的代码:

    // Assuming that you've dequeued/created a UITableViewCell...

    // Check to see if we have the profile image of this account
    UIImage *profileImage = nil;
    NSDictionary *info = [self.twitterProfileInfos objectForKey:account.identifier];
    if (info) profileImage = [info objectForKey:kTwitterProfileImageKey];

    if (profileImage) {
        // You'll probably want some neat code to round the corners of the UIImageView
        // for the top/bottom cells of a grouped style `UITableView`.
        cell.imageView.image = profileImage;

    } else {
        [self getTwitterProfileImageForAccount:account completion:^ {
            // Reload this row
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }];            
    }
Run Code Online (Sandbox Code Playgroud)

所有这一切都是UIImage从字典词典访问一个对象,由帐户标识符键入,然后是静态NSString键.如果它没有获得图像对象,则它调用实例方法,传入完成处理程序块,重新加载表行.实例方法看起来有点像这样:

#pragma mark - Twitter

- (void)getTwitterProfileImageForAccount:(ACAccount *)account completion:(void(^)(void))completion {

    // Create the URL
    NSURL *url = [NSURL URLWithString:@"users/profile_image" relativeToURL:kTwitterApiRootURL];

    // Create the parameters
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            account.username, @"screen_name", 
                            @"bigger", @"size",
                            nil];

    // Create a TWRequest to get the the user's profile image
    TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];

    // Execute the request
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

        // Handle any errors properly, not like this!        
        if (!responseData && error) {
            abort();
        }

        // We should now have some image data
        UIImage *profileImg = [UIImage imageWithData:responseData];

        // Get or create an info dictionary for this account if one doesn't already exist
        NSMutableDictionary *info = [self.twitterProfileInfos objectForKey:account.identifier];
        if (!info) {
            info = [NSMutableDictionary dictionary];            
            [self.twitterProfileInfos setObject:info forKey:account.identifier];
        }

        // Set the image in the profile
        [info setObject:profileImg forKey:kTwitterProfileImageKey];

        // Execute our own completion handler
        if (completion) dispatch_async(dispatch_get_main_queue(), completion);
    }];
}
Run Code Online (Sandbox Code Playgroud)

因此,请确保您正常失败,但是,这将在下载配置文件图像时更新表.在您的完成处理程序中,您可以将它们放在图像缓存中,或者将它们保留在类的生命周期之外.

可以使用相同的过程来访问其他Twitter用户信息,查看他们的文档.