[英]iOS : How to fetch tweet from Twitter API 1.1
Twitter已棄用其API 1.0並實施了新的API 1.1根據新的API,我們需要在獲取公共時間線之前進行身份驗證。
我在這里閱讀了新的API文檔: https : //dev.twitter.com/docs/api/1.1但我不明白,如何在iOS中實現它。
任何人都可以告訴我使用API 1.1獲取Twitter的公共時間表形式的最佳方式
提前致謝。
首先,您需要Authenticate
您的請求(獲取權限)。
第二,請參閱以下步驟:
1.下載FHSTwitterEngine
Twitter庫。
2.將文件夾FHSTwitterEngine
“添加到您的項目和#import "FHSTwitterEngine.h".
3.add 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;
}
});
}
});
}
就這樣。 我剛剛從search Query
中獲取了screen_name
,您可以使用以下方法為用戶獲取時間軸:
// 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
您的應用程序。
您可能對以下庫感興趣。
https://github.com/nst/STTwitter
它是Twitter REST API 1.1的輕量級Objective-C包裝器。
查看自述文件和演示項目的示例
這只是圖書館擁有的眾多方法之一:
- (void)getUserTimelineWithScreenName:(NSString *)screenName
successBlock:(void(^)(NSArray *statuses))successBlock
errorBlock:(void(^)(NSError *error))errorBlock;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.