使用 Twitter API 1.1 显示个人资料照片

问题描述 投票:0回答:2

我们正在开发 iOS 7 Twitter 客户端。我没有太多使用 Twitter API,而且我所做的都是在 1.1 之前。

有人可以帮助我们将个人资料照片加载到我们应用程序的时间轴上吗?

我们的代码如下。

这是我们的.h 文件:

    #import <UIKit/UIKit.h>
    #import <Accounts/Accounts.h>
    #import <Social/Social.h>
    #import <Twitter/Twitter.h>

    @interface FirstViewController : UIViewController <UITableViewDataSource ,                 UITableViewDelegate> {
        UIRefreshControl *myRefreshControl;
    }

    @property (nonatomic) IBOutlet UITableView *timelineTableView;
    @property (nonatomic) NSArray *timelineArray;

    @end

这是我们的应用程序时间表的 .m。

    @interface FirstViewController ()

    @end

    @implementation FirstViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self getTimeline];
        myRefreshControl = [[UIRefreshControl alloc]init];
        myRefreshControl.tintColor = [UIColor blackColor];
        [myRefreshControl setAttributedTitle:[[NSAttributedString         alloc]initWithString:@"Pull to Refresh"]];
        [myRefreshControl addTarget:self action:@selector(refreshTimeline) forControlEvents: UIControlEventValueChanged];
        [self.timelineTableView addSubview:myRefreshControl];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    -(void)getTimeline
    {
        ACAccountStore *account = [[ACAccountStore alloc] init];
        ACAccountType *accountType = [account
                                                      accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

        [account requestAccessToAccountsWithType:accountType
                                         options:nil completion:^(BOOL granted, NSError *error)
         {
             if (granted == YES)
             {
                 NSArray *arrayOfAccounts = [account
                                             accountsWithAccountType:accountType];

                 if ([arrayOfAccounts count] > 0)
                 {
                     ACAccount *twitterAccount = [arrayOfAccounts lastObject];

                     NSURL *requestURL = [NSURL         URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"];

                     NSMutableDictionary *parameters =
                     [[NSMutableDictionary alloc] init];
                     [parameters setObject:@"200" forKey:@"count"];
                     [parameters setObject:@"1" forKey:@"include_entities"];

                     SLRequest *postRequest = [SLRequest
                                               requestForServiceType:SLServiceTypeTwitter
                                               requestMethod:SLRequestMethodGET
                                               URL:requestURL parameters:parameters];

                     postRequest.account = twitterAccount;

                     [postRequest performRequestWithHandler:
                      ^(NSData *responseData, NSHTTPURLResponse
                        *urlResponse, NSError *error)
                      {
                          self.timelineArray = [NSJSONSerialization
                                                JSONObjectWithData:responseData
                                                options:NSJSONReadingMutableLeaves
                                                error:&error];

                          if (self.timelineArray.count != 0) {
                              dispatch_async(dispatch_get_main_queue(), ^{
                                  [self.timelineTableView reloadData];
                              });
                          }
                      }];
                 }
             } else {
             }
         }];
    }

    -(void)refreshTimeline
    {
        [self getTimeline];
        [self.timelineTableView reloadData];
    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [self.timelineArray count];
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle         reuseIdentifier:CellIdentifier];
        }
        NSDictionary *tweet = self.timelineArray[[indexPath row]];
        cell.textLabel.text = [[tweet objectForKey:@"user"]objectForKey:@"name"];
        cell.detailTextLabel.text = [tweet objectForKey:@"text"];
        cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:        [[tweet objectForKey:@"user"]objectForKey:@"profile_image_url"]]];

    return cell;

    }
    @end
ios json objective-c twitter
2个回答
0
投票

回复:
http://api.twitter.com/1/statuses/home_timeline.json
将返回主页提要。它包含一个

user
键,您必须通过
profile_image_url
访问它并获取个人资料图像。

处理字典数组中的响应将解决您的问题,每个字典都会有

user
键,其中包含
profile_image_url


0
投票

您对 api 的调用引用版本 1。我建议您查看 https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline 中的信息并检查响应格式。

您可以深入响应以到达“用户”对象并从那里获取个人资料图像。

© www.soinside.com 2019 - 2024. All rights reserved.