Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Created November 27, 2013 10:37
Show Gist options
  • Save sag333ar/7673694 to your computer and use it in GitHub Desktop.
Save sag333ar/7673694 to your computer and use it in GitHub Desktop.
STTimeline_twitter_api.m: Header-file for fetching tweets from twitter which works for iOS 7 + iOS 6 + iOS 5
//
// timeline_twitter_api.m
// Twitter demo
//
// Created by SagarRK on 09/10/13.
// Copyright (c) 2013 http://sugartin.info . All rights reserved.
//
#import "timeline_twitter_api.h"
#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>
#define SYSTEM_VERSION [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue]
@interface timeline_twitter_api ()
@property (nonatomic, strong) NSString *strTwitterName;
@end
@implementation timeline_twitter_api
- (void)accountStore:(ACAccountStore *)accountStore accountType:(ACAccountType *)accountType granted:(BOOL)granted error:(NSError*)error recentTweetsCount:(NSUInteger)count{
NSString * url1 = [NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=%@&count=%i",self.strTwitterName,count];
__block NSMutableArray *tweetsArray=[NSMutableArray array];
__block timeline_twitter_api *obj = self;
if (granted) {
NSArray *accounts = [accountStore accountsWithAccountType:accountType];
// Check if the users has setup at least one Twitter account
if (accounts.count > 0) {
ACAccount *twitterAccount = [accounts objectAtIndex:0];
if (SYSTEM_VERSION<6 && SYSTEM_VERSION>=5) {
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:url1] parameters:nil requestMethod:TWRequestMethodGET];
postRequest.account = twitterAccount;
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSArray *publicTimeline;
NSError *jsonParsingError = nil;
if (responseData) {
publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
}
if ([publicTimeline isKindOfClass:[NSArray class]]) {
for (NSDictionary *dOfTweet in publicTimeline) {
[tweetsArray addObject:[dOfTweet valueForKey:@"text"]];
}
NSLog(@"tweetsArray is %@",tweetsArray);
[obj performSelectorOnMainThread:@selector(api_twitter_successed:) withObject:tweetsArray waitUntilDone:NO];
//[obj.delegate timeline_twitter_api_did_complete:[NSArray arrayWithArray:tweetsArray]];
}
}];
} else {
SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"] parameters:
[NSDictionary dictionaryWithObjectsAndKeys:self.strTwitterName,@"screen_name",[NSString stringWithFormat:@"%i",count],@"count",nil]];
[twitterInfoRequest setAccount:twitterAccount];
// Making the request
[twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSArray *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
for (NSDictionary *dOfTweet in TWData) {
[tweetsArray addObject:[dOfTweet valueForKey:@"text"]];
}
[obj performSelectorOnMainThread:@selector(api_twitter_successed:) withObject:tweetsArray waitUntilDone:NO];
}];
}
} else {
NSError *error = [NSError errorWithDomain:@"Twitter access denied" code:1 userInfo:nil];
[obj performSelectorOnMainThread:@selector(timeline_twitter_api_did_fail:) withObject:error waitUntilDone:NO];
}
} else {
NSError *error = [NSError errorWithDomain:@"Twitter access denied" code:1 userInfo:nil];
[obj performSelectorOnMainThread:@selector(timeline_twitter_api_did_fail:) withObject:error waitUntilDone:NO];
}
}
- (void)api_twitter_successed:(NSArray*)arrayOfTweets {
[self.delegate timeline_twitter_api_did_complete:[NSArray arrayWithArray:arrayOfTweets]];
}
- (void)timeline_twitter_api_did_fail:(NSError*)error {
[self.delegate timeline_twitter_api_did_fail:error];
}
- (void)getTweetsFortwitterID:(NSString *)twitterName recentTweetsCount:(NSUInteger)count{
self.strTwitterName = twitterName;
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if (SYSTEM_VERSION<6 && SYSTEM_VERSION>=5) {
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted,NSError *error){
[self accountStore:accountStore accountType:accountType granted:granted error:error recentTweetsCount:count];
}];
} else {
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){
[self accountStore:accountStore accountType:accountType granted:granted error:error recentTweetsCount:count];
}];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment