objective-c – 如何在iOS 5中使用Twitter框架获取用户的Twitter
发布时间:2020-12-16 10:21:46 所属栏目:百科 来源:网络整理
导读:我可以使用以下代码发布到Twitter: TWTweetComposeViewController *tweeter = [[TWTweetComposeViewController alloc] init]; [tweeter setInitialText:@"message"]; [tweeter addImage:image]; [self presentModalViewController:tweeter animated:YES];
我可以使用以下代码发布到Twitter:
TWTweetComposeViewController *tweeter = [[TWTweetComposeViewController alloc] init]; [tweeter setInitialText:@"message"]; [tweeter addImage:image]; [self presentModalViewController:tweeter animated:YES]; 如何在iOS 5中使用Twitter框架获取用户的Twitter个人资料信息? 解决方法
好吧,我们说你想在桌面上显示用户在他们设备上的推特账号.您可能希望在表格单元格中显示头像,在这种情况下,您需要查询Twitter的API.
假设您有一个ACAccount对象的NSArray,您可以创建一个字典来存储每个帐户的额外配置文件信息.你的表视图控制器的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]; }]; } 所有这一切都是从字典词典访问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); }]; } 因此,请确保您正常失败,但是,这将在下载配置文件图像时更新表.在您的完成处理程序中,您可以将它们放在图像缓存中,或者将它们保留在类的生命周期之外. 可以使用相同的过程来访问其他Twitter用户信息,see their docs. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |