After experimenting with the Fabric SDK, I was successful in integrating it. I came with some conclusions and I want to share with you guys.
1) When you first logged in to Twitter successfully, a TWTRSession was created for the user. It lasts even after the application is closed and reopened.
2) If a session has already been created for you, and you are trying to log in to get another session object without logging out, an authentication error will be returned.
3) You can check if the session exists or not:
if([[Twitter sharedInstance] session]) { NSLog(@"session already present!!!"); NSLog(@"signed in as %@", [[[Twitter sharedInstance] session] userName]); } else { NSLog(@"you need to login!!"); }
4) I recommend logging in with
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error)];
instead:
[TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error)];
Use the Twitter login button only if you are sure that a session is absent currently.
5) If Twitter authentication really teases you, uninstall the application and try with a fresh installation. This is the last decision!
6) To log out, use [[Twitter sharedInstance] logOut];
The coding part:
I assume that you have already completed all the steps of the mac mac application.
Log in first, then query the timeline.
-(void) loginUserToTwitter { if([[Twitter sharedInstance] session]) { NSLog(@"session already present!!!"); NSLog(@"signed in as %@", [[[Twitter sharedInstance] session] userName]); [self getUserTimeline]; } else { NSLog(@"session not found. Make new request!"); [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) { if(error) NSLog(@"error occurred... %@",error.localizedDescription); else { NSLog(@"Successfully logged in with session :%@",session); [self getUserTimeline]; } }]; } } -(void) getUserTimeline { NSURLRequest *request = [[[Twitter sharedInstance] APIClient] URLRequestWithMethod:@"GET" URL:@"https://api.twitter.com/1.1/statuses/user_timeline.json" parameters:@{@"userid": [Twitter sharedInstance].session.userID, @"count" : @"5", @"screen_name" : [Twitter sharedInstance].session.userName} error:nil]; NSURLResponse *response; NSError *error; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if(!data) { NSLog(@"error....: %@",error.localizedDescription); } else { NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",string); [twitterResponse removeAllObjects]; NSArray *arrayRep = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; twitterResponse = [NSMutableArray arrayWithArray:[TWTRTweet tweetsWithJSONArray:arrayRep]]; [_tableView reloadData]; } }
I would rather use the Twitter SDK method to extract tweets using [TWTRTweet tweetsWithJSONArray:arrayRep] instead of Restkit . It will be very easy to handle here.
Show Twitter Tweets Standard Style:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // Setup tableview self.tableView.estimatedRowHeight = 150; self.tableView.rowHeight = UITableViewAutomaticDimension; // Explicitly set on iOS 8 if using automatic row height calculation self.tableView.allowsSelection = NO; [self.tableView registerClass:[TWTRTweetTableViewCell class] forCellReuseIdentifier:@"TweetCell"]; }
Note:
Download the Fabric SDK from here . You will need to enter an email address. They will email you a download link, you need to follow a few steps. The Fabric Mac app offers you to fully configure your xcode project.
Hope this helps!
Literature:
Twitter login
Show tweets
Cannonball Project Example