0

We're working on an iOS 7 Twitter client. I haven't worked with the the Twitter API much and what I did was before 1.1.

Could somebody please help us get the profile photos loading on our application's Timeline?

Our code is below.

Here is our .h file:

    #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

and here is our .m for the application's timeline.

    @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
4

2 回答 2

0

的响应:
http
://api.twitter.com/1/statuses/home_timeline.json 将返回主页提要。它包含一个user密钥,您必须访问它并获取配置文件图像profile_image_url

处理字典数组中的响应将解决您的问题,并且每个字典都将具有user包含profile_image_url.

于 2013-08-11T07:17:25.157 回答
0

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

您可以深入响应以到达“用户”对象并从那里获取配置文件图像。

于 2013-08-12T12:58:48.407 回答