Advertisement
Guest User

yeah yeah

a guest
Apr 20th, 2014
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  NTRTwitterClient.m
  3. //  TwitterLoginWithParseExample
  4. //
  5. //  Created by Natasha Murashev on 4/6/14.
  6. //  Copyright (c) 2014 NatashaTheRobot. All rights reserved.
  7. //
  8.  
  9. #import "NTRTwitterClient.h"
  10. #import "PFTwitterUtils+NativeTwitter.h"
  11. #import <Accounts/Accounts.h>
  12. #import "FHSTwitterEngine.h"
  13. #import <Parse/Parse.h>
  14.  
  15. @implementation NTRTwitterClient
  16.  
  17. + (void)loginUserWithAccount:(ACAccount *)twitterAccount
  18. {
  19.     [PFTwitterUtils initializeWithConsumerKey:@"<key omitted>" consumerSecret:@"<key omitted>"];
  20.     // PFTwitterUtils Consumer Keys and Secrets.
  21.    
  22.     [PFTwitterUtils setNativeLogInSuccessBlock:^(PFUser *parseUser, NSString *userTwitterId, NSError *error) {
  23.         [self onLoginSuccess:parseUser];
  24.     }];
  25.    
  26.     [PFTwitterUtils setNativeLogInErrorBlock:^(TwitterLogInError logInError) {
  27.         NSError *error = [[NSError alloc] initWithDomain:nil code:logInError userInfo:@{@"logInErrorCode" : @(logInError)}];
  28.         [self onLoginFailure:error];
  29.     }];
  30.    
  31.     [PFTwitterUtils logInWithAccount:twitterAccount];
  32. }
  33.  
  34. + (void)loginUserWithTwitterEngine
  35. {
  36.     [PFTwitterUtils initializeWithConsumerKey:@"<key omitted>" consumerSecret:@"<key omitted>"];
  37.    
  38.     FHSTwitterEngine *twitterEngine = [FHSTwitterEngine sharedEngine];
  39.     FHSToken *token = [FHSTwitterEngine sharedEngine].accessToken;
  40.    
  41.     [PFTwitterUtils logInWithTwitterId:twitterEngine.authenticatedID
  42.                             screenName:twitterEngine.authenticatedUsername
  43.                              authToken:token.key
  44.                        authTokenSecret:token.secret
  45.                                  block:^(PFUser *user, NSError *error) {
  46.                                      if (user) {
  47.                                          [self onLoginSuccess:user];
  48.                                      } else {
  49.                                          [self onLoginFailure:error];
  50.                                      }
  51.                                  }];
  52. }
  53.  
  54. #pragma mark - Private
  55.  
  56. + (void)onLoginSuccess:(PFUser *)user
  57. {
  58.     // Handle Login Success
  59. }
  60.  
  61. + (void)onLoginFailure:(NSError *)error
  62. {
  63.     // Handle Login Failure
  64. }
  65.  
  66. + (void)fetchDataForUser:(PFUser *)user username:(NSString *)twitterUsername
  67. {
  68.     NSString * requestString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/show.json?screen_name=%@", twitterUsername];
  69.    
  70.     NSURL *verify = [NSURL URLWithString:requestString];
  71.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:verify];
  72.     [[PFTwitterUtils twitter] signRequest:request];
  73.    
  74.     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
  75.         NSError *error;
  76.         NSDictionary* result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
  77.         if (!error) {
  78.             user.username = result[@"screen_name"];
  79.             user[@"name"]= result[@"name"];
  80.             user[@"profileDescription"] = result[@"description"];
  81.             user[@"imageURL"] = [result[@"profile_image_url_https"] stringByReplacingOccurrencesOfString:@"_normal" withString:@"_bigger"];
  82.             [user saveEventually];
  83.         }
  84.     }];
  85.    
  86. }
  87.  
  88. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement