Advertisement
thieumao

Json demo and turn off keyboard

May 20th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewDidLoad {
  10.     [super viewDidLoad];
  11.    
  12. //    - category_id, type: integer
  13. //    - option, type: string("all_word", "learned" or "no_learn")
  14. //    - page, type: integer, numericality: {greater_than: 0}, optional
  15. //    - per_page, type: integer, optional, default: 10
  16. //    - auth_token, type: string, presence: true
  17. //    NSString *post = [NSString stringWithFormat:@""];
  18. //                      //@"category_id=%d&option=%@page=%d&per_page=%d&auth_token=%@",15,@"all_word",1,1,@"sRT915Jqr8A2bg1v0m-MHg"];
  19. //    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  20. //    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
  21. //    
  22.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  23.     [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://manh-nt.herokuapp.com/words.json?category_id=15&option=all_word&page=1&per_page=1&auth_token=sRT915Jqr8A2bg1v0m-MHg"]]];
  24.     [request setHTTPMethod:@"GET"];
  25. //    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  26.     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
  27. //    [request setHTTPBody:postData];
  28.     NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
  29.     if(conn)
  30.     {
  31.         NSLog(@"Connection Successful");
  32.     }
  33.     else
  34.     {
  35.         NSLog(@"Connection could not be made");
  36.     }
  37.    
  38. }
  39.  
  40. // This method is used to receive the data which we get using post method.
  41. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {
  42.     NSLog(@"didReceiveData");
  43.     NSString* myString;
  44.     myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
  45.     NSLog(@"%@",myString);
  46. }
  47.  
  48. // This method receives the error report in case of connection is not made to server.
  49. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  50.     NSLog(@"error");
  51. }
  52.  
  53. // This method is used to process the data after connection has made successfully.
  54. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  55.     NSLog(@"connectionDidFinishLoading");
  56. }
  57.  
  58. - (IBAction)signinClicked:(id)sender {
  59. }
  60.  
  61. - (IBAction)TurnOffKeyboard:(id)sender {
  62.     [self.view endEditing:YES];
  63.  
  64. }
  65.  
  66.  
  67. -(BOOL) textFieldShouldReturn:(UITextField *)textField {
  68.     [textField resignFirstResponder];
  69.     return YES;
  70. }
  71. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement