Интуит. Разработка приложений для iOS. Лекция 7. Работа с сетью

  • Published on
    28-Nov-2014

  • View
    667

  • Download
    7

Embed Size (px)

DESCRIPTION

 

Transcript

<ul><li> 1. iOS 7 gleb34@gmail.com </li></ul> <p> 2. 3. Storyboard, ? 4. Storyboard, ? , ; is initial controller 5. UILabel storyboard 6. UILabel storyboard label storyboard , , IBOutlet 7. storyboard ? 8. storyboard ? IBAction 9. , navigation ? 10. , navigation ? Storyboard , push 11. ( ) 12. BasicBlock action = ^{ NSLog(@"Hello, world!"); }; action(); 13. StringBlock action = ^(NSString *str) { NSLog(@"Hello, %@", str); }; action(@"world!"); - 14. LoadBlock action = ^(BOOL success, NSArray *arr) { if (success) { self.array = arr; [self.tableView reloadData]; } else { self.errorLabel.hidden = NO; } }; Server *s = [[Server alloc] init]; [s loadUsersWithAction:action]; 15. - (void)loadUsersWithAction:(LoadBlock)action { NSArray *users = ...; if (users) { action(YES, users); } else { action(NO, nil); } } 16. typedef void(^BasicBlock)(); typedef void(^StringBlock)(NSString *str); typedef void(^LoadBlock)(BOOL success, NSArray *arr); 17. http://fuckingblocksyntax.com 18. 19. // Main thread dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Background thread dispatch_async(dispatch_get_main_queue(), ^{ // Main thread }); }); , 20. - (void)viewDidLoad { // Main thread [self.activityIndicator startAnimating]; self.server = [[Server alloc] init]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ . // Background thread self.users = [self.server loadUsers]; dispatch_async(dispatch_get_main_queue(), ^{ . // Main thread [self.activityIndicator stopAnimating]; if (self.users.count &gt; 0) { [self.tableView reloadData]; self.tableView.hidden = NO; } else { self.emptyLabel.hidden = NO; } }); }); } 21. 22. NSURL *url = [NSURL URLWithString:@"http://intuit.ru"]; NSString *str = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; . NSLog(@"%@", str); NSURL *url = [NSURL URLWithString:@"http://www.intuit.ru/logo.png"]; NSData *data = [[NSData alloc] initWithContentsOfURL:url]; . NSLog(@"%@", data); 23. JSON 24. {! "firstName": "",! "lastName": "",! "address": {! "streetAddress": " ., 101, .101",! "city": "",! "postalCode": 101101! },! "phoneNumbers": [! "812 123-1234",! "916 123-4567"! ]! } http://ru.wikipedia.org/wiki/JSON 25. NSArray *arr = @[ @"str1", @"str2", @3 ]; NSData *data = [NSJSONSerialization dataWithJSONObject:arr options:0 error:nil]; . NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; . NSLog(@"%@", json); json 26. NSString *json = ...; NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"%@", arr); json 27. JSON dropbox dropbox www dl 28. CocoaPods ( ) 29. http://cocoapods.org ( &gt; &gt; ): . $ sudo gem install cocoapods . . , : . $ pod search AFNetworking , sudo gem install rake Y, , 6 sudo gem install cocoapods 30. ? Podle pod pod install .xcodeproj , .xcworkspace 31. 32. HTTP - GET-: . http://site.com/folder/le.php?param1=v1&amp;param2=v2 POST-: . http://site.com/folder/le.php. . param1 = v1. param2 = v2 33. (GET). http://site.com/api/shops.php. . (GET). http://site.com/api/shops.php?category=auto. . (POST). http://site.com/api/login.php. login = user. password = 12345. . (POST). http://site.com/api/send_comment.php. shop_id = 123. comment_text = . token = asdasdasdasd 34. (GET). http://site.com/api/shops.php. {. "shops": [. {. "title": "",. "location": "50.12321,30.12324",. "rating": 4.5,. "comments_count": 20. },. {. "title": " ",. "location": "51.21321,34.132",. "rating": 4.8,. "comments_count": 17. }. ]. } 35. (POST). http://site.com/api/login.php {. "result" : "error",. "error" : " ". } {. "result" : "success",. "token" : "kjhkdsfhsdofsdfsdf". } 36. (POST). http://site.com/api/send_comment.php {. "result" : "success". } 37. AFNetworking ( ) 38. @interface ViewController () . @property (strong, nonatomic) AFHTTPSessionManager *manager; . @end - (void)viewDidLoad { [super viewDidLoad]; NSURL *baseUrl = [NSURL URLWithString:@"http:// api.worldweatheronline.com/free/v1/"]; self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseUrl]; self.manager.responseSerializer = [AFJSONResponseSerializer serializer]; } #import 39. NSDictionary *params = @{ @"q" : @"Moscow", @"format" : @"json", @"num_of_days" : @1, @"key" : @"hnhq9j8ta97x5d9hbap7sk5q" }; . [self.manager GET:@"weather.ashx" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) { NSDictionary *result = responseObject; NSLog(@"%@", result); } failure:^(NSURLSessionDataTask *task, NSError *error) { }]; GET- 40. NSDictionary *params = @{ @"q" : @"Moscow", @"format" : @"json", @"num_of_days" : @1, @"key" : @"hnhq9j8ta97x5d9hbap7sk5q" }; . [self.manager POST:@"weather.ashx" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) { NSDictionary *result = responseObject; NSLog(@"%@", result); } failure:^(NSURLSessionDataTask *task, NSError *error) { }]; POST- 41. ( , , json). NSString NSData, AFNetworking 42. ! gleb34@gmail.com twitter.com/pilot34 </p>

Recommended

View more >