0

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
4

2 に答える 2

0

API への呼び出しはバージョン 1 を参照しています。https://dev.twitter.com/docs/api/1.1/get/statuses/home_timelineで情報を確認し、応答形式を調べることをお勧めします。

応答をドリルダウンして「ユーザー」オブジェクトに到達し、そこからプロフィール画像を取得できます。

于 2013-08-12T12:58:48.407 に答える