Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 9.83 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  2.     // Return the number of sections.
  3.     return 1;
  4. }
  5.  
  6.  
  7. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  8.     // Return the number of rows in the section.
  9.     return [tweets count];
  10. }
  11.  
  12. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  13. {
  14.     return 80;
  15. }
  16.  
  17.  
  18. // Customize the appearance of table view cells.
  19. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  20.    
  21.     static NSString *CellIdentifier = @"Cell";
  22.    
  23.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  24.     if (cell == nil) {
  25.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
  26.     }
  27.    
  28.     // Configure the cell...
  29.        
  30.         NSDictionary *thetweet = [tweetss objectAtIndex:[indexPath row]];
  31.        
  32.     cell.textLabel.text = [thetweet objectForKey:@"text"];
  33.         cell.textLabel.adjustsFontSizeToFitWidth = YES;
  34.         cell.textLabel.font = [UIFont systemFontOfSize:12];
  35.         cell.textLabel.minimumFontSize = 10;
  36.         cell.textLabel.numberOfLines = 4;
  37.         cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
  38.        
  39.  
  40. }
  41.  
  42.  
  43.  
  44. /*
  45.  // The designated initializer.  Override if you create the controller programmatically and
  46.  // want to perform customization that is not appropriate for viewDidLoad.
  47. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  48.     if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
  49.         // Custom initialization
  50.     }
  51.     return self;
  52. }
  53. */
  54.  
  55.  
  56. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  57. - (void)viewDidLoad {
  58.     [super viewDidLoad];
  59.  
  60.         self.title = @"Hello OAuth-Twitter";
  61.  
  62.         oAuth = [[OAuth alloc] initWithConsumerKey:OAUTH_CONSUMER_KEY andConsumerSecret:OAUTH_CONSUMER_SECRET];
  63.         [oAuth loadOAuthTwitterContextFromUserDefaults];
  64.            
  65.     [self resetUi];
  66.     [tweets setFont:[UIFont systemFontOfSize:12]];
  67.    
  68. }
  69.  
  70. - (void)resetUi {
  71.     if (oAuth.oauth_token_authorized) {
  72.         tweets.hidden = NO;
  73.         uploadMediaButton.hidden = NO;
  74.         tweets.text = @"";
  75.         latestTweetsButton.hidden = NO;
  76.         signedInAs.text = [NSString stringWithFormat:@"Logged in as %@.", oAuth.screen_name];
  77.         NSLog(@"Resetting UI to authorized state. Twitter user: %@", oAuth.screen_name);
  78.         postButton.enabled = YES;
  79.         statusText.enabled = YES;
  80.        
  81.         UIBarButtonItem *logout = [[UIBarButtonItem alloc] initWithTitle:@"Log out"
  82.                                                                    style:UIBarButtonItemStyleBordered
  83.                                                                   target:self
  84.                                                                   action:@selector(logout)];
  85.         self.navigationItem.rightBarButtonItem = logout;
  86.         [logout release];
  87.        
  88.     } else {
  89.         tweets.text = @"";
  90.         tweets.hidden = YES;
  91.         latestTweetsButton.hidden = YES;
  92.         uploadMediaButton.hidden = YES;
  93.         tweets.text = @"";
  94.         signedInAs.text = @"";
  95.         NSLog(@"Resetting UI to non-authorized state.");
  96.        
  97.         postButton.enabled = NO;
  98.         statusText.text = @"";
  99.         statusText.enabled = NO;
  100.        
  101.         UIBarButtonItem *login = [[UIBarButtonItem alloc] initWithTitle:@"Log in"
  102.                                                                    style:UIBarButtonItemStyleBordered
  103.                                                                   target:self
  104.                                                                   action:@selector(login)];
  105.         self.navigationItem.rightBarButtonItem = login;
  106.         [login release];
  107.        
  108.     }
  109.    
  110.     [statusText resignFirstResponder];
  111. }
  112.  
  113. // Override to allow orientations other than the default portrait orientation.
  114. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
  115.    
  116.     // Supports all but upside-down orientation.
  117.         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  118. }
  119.  
  120.  
  121. - (void)didReceiveMemoryWarning {
  122.     // Releases the view if it doesn't have a superview.
  123.     [super didReceiveMemoryWarning];
  124.    
  125.     // Release any cached data, images, etc that aren't in use.
  126. }
  127.  
  128. - (void)viewDidUnload {
  129.     [super viewDidUnload];
  130.     // Release any retained subviews of the main view.
  131.     // e.g. self.myOutlet = nil;
  132. }
  133.  
  134.  
  135. - (void)dealloc {
  136.    
  137.     [oAuth release];
  138.    
  139.     [super dealloc];
  140. }
  141.  
  142. #pragma mark -
  143. #pragma mark Button actions
  144.  
  145. - (void)login {
  146.     if (!loginPopup) {
  147.         loginPopup = [[CustomLoginPopup alloc] initWithNibName:@"TwitterLoginPopup" bundle:nil];        
  148.         loginPopup.oAuth = oAuth;
  149.         loginPopup.delegate = self;
  150.         loginPopup.uiDelegate = self;
  151.     }
  152.     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginPopup];
  153.     [self presentModalViewController:nav animated:YES];        
  154.     [nav release];
  155. }
  156.  
  157. - (void)logout {
  158.     [oAuth forget];
  159.     [oAuth saveOAuthTwitterContextToUserDefaults];
  160.     [self resetUi];
  161. }
  162.  
  163. //- (IBAction)didPressPost:(id)sender {
  164. //    
  165. //    // We assume that the user is authenticated by this point and we have a valid OAuth context,
  166. //    // thus no need to do context checking.
  167. //    
  168. //    NSString *postUrl = @"https://api.twitter.com/1/statuses/update.json";
  169. //    
  170. //    ASIFormDataRequest *request = [[ASIFormDataRequest alloc]
  171. //                                   initWithURL:[NSURL URLWithString:postUrl]];
  172. //    [request setPostValue:statusText.text forKey:@"status"];
  173. //    
  174. //    [request addRequestHeader:@"Authorization"
  175. //                        value:[oAuth oAuthHeaderForMethod:@"POST"
  176. //                                                   andUrl:postUrl
  177. //                                                andParams:[NSDictionary dictionaryWithObject:statusText.text
  178. //                                                                                      forKey:@"status"]]];
  179. //    
  180. //    [request startSynchronous];
  181. //    
  182. //    NSLog(@"Status posted. HTTP result code: %d", request.responseStatusCode);
  183. //    
  184. //    statusText.text = @"";
  185. //    
  186. //    [request release];
  187. //    
  188. //    [statusText resignFirstResponder];
  189. //}
  190.  
  191. - (IBAction)didPressLatestTweets:(id)sender {
  192.     NSString *getUrl = @"http://api.twitter.com/1/statuses/user_timeline.json";
  193.    
  194.     NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"5", @"count", nil];
  195.    
  196.     // Note how the URL is without parameters here...
  197.     // (this is how OAuth works, you always give it a "normalized" URL without parameters
  198.     // since you give parameters separately to it, even for GET)
  199.     NSString *oAuthValue = [oAuth oAuthHeaderForMethod:@"GET" andUrl:getUrl andParams:params];
  200.    
  201.     // ... but the actual request URL contains normal GET parameters.
  202.     ASIHTTPRequest *request = [[ASIHTTPRequest alloc]
  203.                                initWithURL:[NSURL URLWithString:[NSString
  204.                                                                  stringWithFormat:@"%@?count=%@",
  205.                                                                  getUrl,
  206.                                                                  [params valueForKey:@"count"]]]];
  207.     [request addRequestHeader:@"Authorization" value:oAuthValue];
  208.     [request startSynchronous];
  209.    
  210.     NSLog(@"Got statuses. HTTP result code: %d", request.responseStatusCode);
  211.    
  212.     tweets.text = @"";
  213.    
  214.     NSArray *gotTweets = [[request responseString] JSONValue];
  215.    
  216.     for (NSDictionary *tweet in gotTweets) {
  217.         tweets.text = [NSString stringWithFormat:@"%@%@\n", tweets.text, [tweet valueForKey:@"text"]];
  218.     }
  219.    
  220.     [request release];
  221.    
  222.     [statusText resignFirstResponder];
  223. }
  224.  
  225. //- (IBAction)didPressUploadMedia:(id)sender {
  226. //    NSLog(@"paskat");
  227. //    UploadMedia *uploadMedia = [[UploadMedia alloc] initWithNibName:@"UploadMedia" bundle:nil];
  228. //    uploadMedia.oAuth = oAuth;
  229. //    [self.navigationController pushViewController:uploadMedia animated:YES];
  230. //    [uploadMedia release];
  231. //}
  232.  
  233.  
  234. #pragma mark -
  235. #pragma mark TwitterLoginPopupDelegate
  236.  
  237. - (void)twitterLoginPopupDidCancel:(TwitterLoginPopup *)popup {
  238.     [self dismissModalViewControllerAnimated:YES];        
  239.     [loginPopup release]; loginPopup = nil; // was retained as ivar in "login"
  240. }
  241.  
  242. - (void)twitterLoginPopupDidAuthorize:(TwitterLoginPopup *)popup {
  243.     [self dismissModalViewControllerAnimated:YES];        
  244.     [loginPopup release]; loginPopup = nil; // was retained as ivar in "login"
  245.     [oAuth saveOAuthTwitterContextToUserDefaults];
  246.     [self resetUi];
  247. }
  248.  
  249. #pragma mark -
  250. #pragma mark TwitterLoginUiFeedback
  251.  
  252. - (void) tokenRequestDidStart:(TwitterLoginPopup *)twitterLogin {
  253.     NSLog(@"token request did start");
  254.     [loginPopup.activityIndicator startAnimating];
  255. }
  256.  
  257. - (void) tokenRequestDidSucceed:(TwitterLoginPopup *)twitterLogin {
  258.     NSLog(@"token request did succeed");    
  259.     [loginPopup.activityIndicator stopAnimating];
  260. }
  261.  
  262. - (void) tokenRequestDidFail:(TwitterLoginPopup *)twitterLogin {
  263.     NSLog(@"token request did fail");
  264.     [loginPopup.activityIndicator stopAnimating];
  265. }
  266.  
  267. - (void) authorizationRequestDidStart:(TwitterLoginPopup *)twitterLogin {
  268.     NSLog(@"authorization request did start");    
  269.     [loginPopup.activityIndicator startAnimating];
  270. }
  271.  
  272. - (void) authorizationRequestDidSucceed:(TwitterLoginPopup *)twitterLogin {
  273.     NSLog(@"authorization request did succeed");
  274.     [loginPopup.activityIndicator stopAnimating];
  275. }
  276.  
  277. - (void) authorizationRequestDidFail:(TwitterLoginPopup *)twitterLogin {
  278.     NSLog(@"token request did fail");
  279.     [loginPopup.activityIndicator stopAnimating];
  280. }
  281.  
  282.  
  283. @end