作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Twitter已弃用其API 1.0,并实施了新的API 1.1
根据新的API,我们需要先进行身份验证,然后才能获取 public 时间轴。
我在这里阅读了新的API文档:https://dev.twitter.com/docs/api/1.1
但我不清楚如何在iOS中实现它。
谁能告诉我使用API 1.1获取公开时间轴表单Twitter的最佳方法
提前致谢。
最佳答案
首先,您需要Authenticate
您的请求(获得许可)。
第二,请参阅以下步骤:
1.下载FHSTwitterEngine
Twitter库。
2.将文件夹FHSTwitterEngine
“添加到您的项目和#import "FHSTwitterEngine.h".
3.将SystemConfiguration.framework
添加到您的项目中。
用法:1.在[ViewDidLoad]
中添加以下代码。
UIButton *logIn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
logIn.frame = CGRectMake(100, 100, 100, 100);
[logIn setTitle:@"Login" forState:UIControlStateNormal];
[logIn addTarget:self action:@selector(showLoginWindow:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:logIn];
[[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:@"<consumer_key>" andSecret:@"<consumer_secret>"];
[[FHSTwitterEngine sharedEngine]setDelegate:self];
and don't forget to import the delegate FHSTwitterEngineAccessTokenDelegate.
you need to get the permission for your request, with the following method which will present Login window:
- (void)showLoginWindow:(id)sender {
[[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:self withCompletion:^(BOOL success) {
NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!");
}];
}
Twitter
用户名和密码以验证您的请求。
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[FHSTwitterEngine sharedEngine]loadAccessToken];
NSString *username = [[FHSTwitterEngine sharedEngine]loggedInUsername];// self.engine.loggedInUsername;
if (username.length > 0) {
lbl.text = [NSString stringWithFormat:@"Logged in as %@",username];
[self listResults];
} else {
lbl.text = @"You are not logged in.";
}
}
- (void)storeAccessToken:(NSString *)accessToken {
[[NSUserDefaults standardUserDefaults]setObject:accessToken forKey:@"SavedAccessHTTPBody"];
}
- (NSString *)loadAccessToken {
return [[NSUserDefaults standardUserDefaults]objectForKey:@"SavedAccessHTTPBody"];
}
4.Now you are ready to get your request, with the following method(in this method I created a `Twitter` search for some `Hashtag`, to get the screen_name for example):
- (void)listResults {
dispatch_async(GCDBackgroundThread, ^{
@autoreleasepool {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// the following line contains a FHSTwitterEngine method wich do the search.
dict = [[FHSTwitterEngine sharedEngine]searchTweetsWithQuery:@"#iOS" count:100 resultType:FHSTwitterEngineResultTypeRecent unil:nil sinceID:nil maxID:nil];
// NSLog(@"%@",dict);
NSArray *results = [dict objectForKey:@"statuses"];
// NSLog(@"array text = %@",results);
for (NSDictionary *item in results) {
NSLog(@"text == %@",[item objectForKey:@"text"]);
NSLog(@"name == %@",[[item objectForKey:@"user"]objectForKey:@"name"]);
NSLog(@"screen name == %@",[[item objectForKey:@"user"]objectForKey:@"screen_name"]);
NSLog(@"pic == %@",[[item objectForKey:@"user"]objectForKey:@"profile_image_url_https"]);
}
dispatch_sync(GCDMainThread, ^{
@autoreleasepool {
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Complete!" message:@"Your list of followers has been fetched" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
});
}
});
}
screen_name
获得
search Query
,您可以使用以下方法为用户获取时间轴:
// statuses/user_timeline
- (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count;
- (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
FHSTwitterEngine.h
以了解您需要使用哪种方法。
<consumer_key>
和
<consumer_secret>
,您需要
register
将您的应用程序
Twitter
。
关于ios - iOS:如何从Twitter API 1.1获取推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17207317/
我想开发一个 Skype 机器人,它将用户名作为输入,并根据用户输入以相反的字符大小写表示hello username。简而言之,如果用户输入他的名字 james,我的机器人会回复他为 Hello J
我是一名优秀的程序员,十分优秀!