Advertisement
vborra

pagetwoviewcontroller.m

May 19th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void)viewWillAppear:(BOOL)animated
  2. {
  3.     [self.twitterTableView reloadData];
  4. }
  5.  
  6. - (void)viewDidLoad
  7. {
  8.     [super viewDidLoad];
  9.     // Do any additional setup after loading the view.
  10.     self.view.backgroundColor = [UIColor clearColor];
  11.     self.view.alpha = 1.0;
  12.    
  13.     self.twitterTableView.delegate = self;
  14.     [mapView.layer setCornerRadius:7.0f];
  15.     [mapView.layer setMasksToBounds:YES];
  16.      mapView.alpha = .7;
  17.     [twitterFrame.layer setCornerRadius:7.0f];
  18.     [twitterFrame.layer setMasksToBounds:YES];
  19.     [twitterTableView.layer setCornerRadius:7.0f];
  20.     [twitterTableView.layer setMasksToBounds:YES];
  21.    
  22.     [self getTwitterData];
  23.  
  24.  
  25. }    
  26. -(void)getTwitterData
  27. {
  28.         ACAccountStore *store = [[ACAccountStore alloc] init];
  29.         ACAccountType *twitterAccountType =
  30.         [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
  31.        
  32.         //  Request access from the user for access to his Twitter accounts
  33.         [store requestAccessToAccountsWithType:twitterAccountType
  34.                          withCompletionHandler:^(BOOL granted, NSError *error) {
  35.                              if (!granted) {
  36.                                  // The user rejected your request
  37.                                  UIAlertView *noAccountAlert = [[UIAlertView alloc] initWithTitle:@"Twitter Access" message:@"To Use The Twitter Section of This App, You Must Allow Acces to Twitter" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  38.                                  [noAccountAlert show];
  39.                              }
  40.                              else {
  41.                                  // Grab the available accounts
  42.                                  NSArray *twitterAccounts =
  43.                                  [store accountsWithAccountType:twitterAccountType];
  44.                                  
  45.                                  if ([twitterAccounts count] > 0) {
  46.                                      // Use the first account for simplicity
  47.                                      ACAccount *account = [twitterAccounts objectAtIndex:0];
  48.                                      NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:@"25" forKey:@"count"];
  49.                                      // Now make an authenticated request to our endpoint
  50.                                      
  51.                                      //  The endpoint that we wish to call
  52.                                      NSURL *url =
  53.                                      [NSURL
  54.                                       URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"];
  55.                                      
  56.                                      //  Build the request with our parameter
  57.                                      TWRequest *request =
  58.                                      [[TWRequest alloc] initWithURL:url
  59.                                                          parameters:params
  60.                                                       requestMethod:TWRequestMethodGET];
  61.                                      
  62.                                      // Attach the account object to this request
  63.                                      [request setAccount:account];
  64.                                      
  65.                                      [request performRequestWithHandler:
  66.                                       ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
  67.                                           if (!responseData) {
  68.                                               // inspect the contents of error
  69.                                               NSLog(@"%@", error);
  70.                                           }
  71.                                           else {
  72.                                               NSError *jsonError;
  73.                                               NSMutableArray *timeline =
  74.                                               [NSJSONSerialization
  75.                                                JSONObjectWithData:responseData
  76.                                                options:NSJSONReadingMutableLeaves
  77.                                                error:&jsonError];            
  78.                                               if (timeline) {                          
  79.                                                   // at this point, we have an object that we can parse
  80.                                                   self.timeLineData = timeline;
  81.                                                  
  82.                                               }
  83.                                               else {
  84.                                                   NSLog(@"%@", jsonError);
  85.                                               }
  86.                                              
  87.                                              
  88.                                           }
  89.                                          
  90.                                       }];
  91.                                      
  92.                                  }
  93.                              }
  94.                              
  95.                          }];    
  96.         [twitterTableView setDelegate:self];
  97.         [twitterTableView reloadData];
  98.  
  99. }
  100.  
  101.  
  102.    
  103.  
  104. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  105. {
  106.  
  107.  
  108.     return 1;
  109. }
  110. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  111. {
  112.  
  113.     return 25;
  114. }
  115. -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117.     return 88.0;
  118. }
  119. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121.     NSLog(@"cell for row at twitter called");
  122.     static NSString *CellIdentifier = @"Cell";
  123.    
  124.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  125.     if (cell == nil) {
  126.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  127.     }
  128.    
  129.     NSDictionary *tweet = [self.timeLineData objectAtIndex:indexPath.row];
  130.     // NSLog(@"Tweet at index %d is %@", [indexPath row], tweet);
  131.     //NSLog(@"Tweet: %@", tweet);
  132.       //  NSLog(@"Text in cellforpath: %@", [tweet objectForKey:@"text"]);
  133.     if ([tweet objectForKey:@"text"] != nil) {
  134.         cell.textLabel.text = [tweet objectForKey:@"text"];
  135.         cell.textLabel.numberOfLines = 4;
  136.         NSDictionary *whoPostedTweet = [tweet objectForKey:@"user"];
  137.         NSLog(@"Name: %@", [whoPostedTweet objectForKey:@"name"]);
  138.         cell.detailTextLabel.text = [whoPostedTweet objectForKey:@"name"];
  139. //        cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [whoPostedTweet objectForKey:@"profile_image_url"]]]]];
  140.                                                                                                          
  141.          return cell;
  142.        
  143.     } else {
  144.         cell.textLabel.text = @"Error";
  145.         [self getTwitterData];
  146.         NSLog(@"nil called(else) ");
  147.         return cell;
  148.     }
  149.    
  150.    
  151.  
  152.  
  153.    
  154. }
  155. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157.    NSLog(@"Touched Table View at Row: %i", indexPath.row);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement