Objective c 预定义要显示的特定twitter提要
在下面的代码中,tableview显示手机用户的twitter提要。我只想显示我在代码中预定义的用户提要。我在网上查了一下,但找不到任何关于这方面的教程。我希望编辑此代码,而不是使用第三方API重新启动 ViewController.mObjective c 预定义要显示的特定twitter提要,objective-c,twitter,ios7.1,acaccount,Objective C,Twitter,Ios7.1,Acaccount,在下面的代码中,tableview显示手机用户的twitter提要。我只想显示我在代码中预定义的用户提要。我在网上查了一下,但找不到任何关于这方面的教程。我希望编辑此代码,而不是使用第三方API重新启动 ViewController.m #import "TwitterViewController.h" #import <Accounts/Accounts.h> #import <Social/Social.h> @interface TwitterViewContro
#import "TwitterViewController.h"
#import <Accounts/Accounts.h>
#import <Social/Social.h>
@interface TwitterViewController ()
@property (strong, nonatomic) NSArray *twitterArray;
@end
@implementation TwitterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self twitterTimeline];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)twitterTimeline {
ACAccountStore *account = [[ACAccountStore alloc] init]; // Creates AccountStore object.
// Asks for the Twitter accounts configured on the device.
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
// If we have access to the Twitter accounts configured on the device we will contact the Twitter API.
if (granted == YES){
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType]; // Retrieves an array of Twitter accounts configured on the device.
// If there is a leat one account we will contact the Twitter API.
if ([arrayOfAccounts count] > 0) {
ACAccount *twitterAccount = [arrayOfAccounts lastObject]; // Sets the last account on the device to the twitterAccount variable.
NSURL *requestAPI = [NSURL URLWithString:@"http://api.twitter.com/1.1/statuses/user_timeline.json"]; // API call that returns entires in a user's timeline.
// The requestAPI requires us to tell it how much data to return so we use a NSDictionary to set the 'count'.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:@"100" forKey:@"count"];
[parameters setObject:@"1" forKey:@"include_entities"];
// This is where we are getting the data using SLRequest.
SLRequest *posts = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestAPI parameters:parameters];
posts.account = twitterAccount;
// The postRequest: method call now accesses the NSData object returned.
[posts performRequestWithHandler:
^(NSData *response, NSHTTPURLResponse
*urlResponse, NSError *error)
{
// The NSJSONSerialization class is then used to parse the data returned and assign it to our array.
self.twitterArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (self.twitterArray.count != 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.twitterFeedTable reloadData]; // Here we tell the table view to reload the data it just recieved.
});
}
}];
}
} else {
// Handle failure to get account access
NSLog(@"%@", [error localizedDescription]);
}
}];
}
#pragma mark Table View Data Source Mehtods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Returns the number of rows for the table view using the array instance variable.
return [_twitterArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Creates each cell for the table view.
static NSString *cellID = @"CELLID" ;
UITableViewCell *cell = [self.twitterFeedTable dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
// Creates an NSDictionary that holds the user's posts and then loads the data into each cell of the table view.
NSDictionary *tweet = _twitterArray[indexPath.row];
cell.textLabel.text = tweet[@"text"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// When a user selects a row this will deselect the row.
[self.twitterFeedTable deselectRowAtIndexPath:indexPath animated:YES];
}
@end
#导入“TwitterViewController.h”
#进口
#进口
@界面TwitterViewController()
@属性(强,非原子)NSArray*twitterArray;
@结束
@TwitterViewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
[自我推特时间线];
//从nib加载视图后,执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)推特时间线{
ACAccountStore*account=[[ACAccountStore alloc]init];//创建AccountStore对象。
//询问设备上配置的Twitter帐户。
ACAccountType*accountType=[帐户类型WithAccountTypeIdentifier:ACAccountTypeIdentifierWitter];
[account requestAccessToAccountsWithType:accountType选项:无完成:^(已授予BOOL,N错误*错误)
{
//如果我们可以访问设备上配置的Twitter帐户,我们将联系Twitter API。
如果(已授予==是){
NSArray*arrayOfAccounts=[account accountsWithAccountType:accountType];//检索设备上配置的Twitter帐户数组。
//如果有leat one帐户,我们将联系Twitter API。
如果([arrayOfAccounts]>0){
ACAccount*twitterAccount=[arrayOfAccounts lastObject];//将设备上的最后一个帐户设置为twitterAccount变量。
NSURL*requestAPI=[NSURL URLWithString:@”http://api.twitter.com/1.1/statuses/user_timeline.json“];//返回用户时间线中的entires的API调用。
//requestAPI要求我们告诉它要返回多少数据,因此我们使用NSDictionary来设置“计数”。
NSMutableDictionary*参数=[[NSMutableDictionary alloc]init];
[parameters setObject:@“100”forKey:@“count”];
[参数setObject:@“1”forKey:@“包含实体”];
//这就是我们使用SLRequest获取数据的地方。
SLRequest*posts=[SLRequestRequestForServiceType:SLServiceTypeTwitter请求方法:SLRequestMethodGET URL:requestAPI参数:参数];
posts.account=推特账号;
//方法调用现在访问返回的NSData对象。
[发布performRequestWithHandler:
^(NSData*响应,NSHTTPURL响应
*URL响应,N错误*错误)
{
//然后使用NSJSONSerialization类解析返回的数据并将其分配给我们的数组。
self.twitterArray=[NSJSONSerialization JSONObjectWithData:响应选项:NSJSONReadingMutableLeaves错误:&错误];
if(self.twitterArray.count!=0){
dispatch\u async(dispatch\u get\u main\u queue()^{
[self.twitterFeedTable reloadData];//这里我们告诉表视图重新加载刚刚接收到的数据。
});
}
}];
}
}否则{
//处理获取帐户访问权限失败
NSLog(@“%@,[错误本地化描述]);
}
}];
}
#pragma标记表视图数据源方法
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//使用数组实例变量返回表视图的行数。
返回[_twitter数组计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//为表视图创建每个单元格。
静态NSString*cellID=@“cellID”;
UITableViewCell*cell=[self.twitterFeedTable dequeueReusableCellWithIdentifier:cellID];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellID];
}
//创建保存用户帖子的NSDictionary,然后将数据加载到表视图的每个单元格中。
NSDictionary*tweet=_twitterArray[indexPath.row];
cell.textlab.text=tweet[@“text”];
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
//当用户选择一行时,将取消选择该行。
[self.twitterFeedTable取消RowatineXpath:indexPath:YES];
}
@结束
信息是
您需要在http://api.twitter.com/1.1/statuses/user_timeline.json
请求,仅此而已
文件中提供的示例:
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi