conceitos e prática no desenvolvimento ios - mobile conf 2014

Download Conceitos e prática no desenvolvimento iOS - Mobile Conf 2014

Post on 23-Aug-2014

574 views

Embed Size (px)

DESCRIPTION

Slides da minha palestra na Mobile Conf 2014.

TRANSCRIPT

  • Conceitos e prtica no desenvolvimento iOS
  • Desenvolvedor e instrutor (iOS, Android, Java e Ruby) Quem sou eu? @fabiopimentel github.com/fabiopimentel
  • Vamos programar? Objective-C (OFICIAL) Ruby - RubyMotion C# - Xamarin
  • iOS MVC Controller Model View
  • iOS MVC Controller Model View -------------- --------------
  • iOS MVC Controller Model View -------------- --------------
  • iOS MVC Controller Model View -------------- -------------- Outlet
  • iOS MVC Controller Model View -------------- -------------- Outlet Action
  • Opes para View
  • StoryBoard
  • Xib
  • Opes para View 1) StoryBoard 2) Xib 2) Programaticamente
  • Ciclo de Vida
  • ViewController A tela j existe?
  • ViewController A tela j existe? viewWillAppear No viewDidAppear Tela visvel viewDidLoad F l u x o
  • ViewController A tela j existe? Sim viewWillAppear No viewDidAppear Tela visvel viewDidLoad
  • viewDidLoad viewWillAppear Tela visvel viewWillDisappear Ciclo de vida - ViewController viewDidAppear viewDidDisappear Tela no visvel
  • Ciclo de Vida - App Aplicao no est rodando Usurio clica no cone da app application:didFinishLauchingWithOptions: applicationDidBecomeActive: Aplicao rodando Usurio clica em Home, ou recebe chamada telefnica/ SMS applicationWillResignActive: applicationDidEnterBackground: Aplicao em backgroung Usurio clica no cone da app applicationWillEnterForeground:
  • Ciclo de Vida - App Aplicao no est rodando Usurio clica no cone da app application:didFinishLauchingWithOptions: applicationDidBecomeActive: Aplicao rodando Usurio clica em Home, ou recebe chamada telefnica/ SMS applicationWillResignActive: applicationDidEnterBackground: Aplicao em backgroung Usurio clica no cone da app applicationWillEnterForeground:
  • Live Coding First Round
  • Principal componente do iOS
  • TableView https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ AboutTableViewsiPhone/AboutTableViewsiPhone.html
  • UITableViewStyleGrouped
  • UITableViewStylePlain
  • Section 0 Section 1 Section 2 Section 3 Section 4
  • Row 0 Row 1
  • Row 0 Row 1 Row 2
  • NSIndexPath "The UIKit framework adds programming interfaces to the NSIndexPath class of the Foundation framework to facilitate the identication of rows and sections in UITableView objects and the identication of items and sections in UICollectionView objects." https://developer.apple.com/library/ios/documentation/uikit/reference/NSIndexPath_UIKitAdditions/Reference/ Reference.html
  • NSIndexPath Row Section
  • Row 2 Section 2 NSIndexPath
  • UITableViewController https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewController_Class/ Reference/Reference.html
  • Mtodos para congurao numberOfSectionsInTableView: - tableView:numberOfRowsInSection: tableView:cellForRowAtIndexPath:
  • Trabalhando em Background
  • Opo 1 performSelectorInBackground:withObject: performSelectonOnMainThread:withObject: waitUntilDone
  • Exemplo -(IBAction)carregaLista:(UIButton *) botao ! { ! [self performSelectorInBackground: @selector(buscaMotos) withObject: nil];! }
  • Exemplo -(IBAction)carregaLista:(UIButton *) botao ! { ! [self performSelectorInBackground: @selector(buscaMotos) withObject: nil];! } -(void)buscaMotos! { //faz a comunicao com algum webservice [self performSelectorOnMainThread @selector(atualizaTabela:) withObject: arrayDeDados waitUntilDone: NO];! }
  • Exemplo -(IBAction)carregaLista:(UIButton *) botao ! { ! [self performSelectorInBackground: @selector(buscaMotos) withObject: nil];! } -(void)buscaMotos! { //faz a comunicao com algum webservice [self performSelectorOnMainThread @selector(atualizaTabela:) withObject: nil waitUntilDone: NO];! }! -(void)atualizaTabela:(NSObject*) object! { ! ! [self.tableView reloadData];! }
  • Opo 2 NSOperationQueue NSInvocationOperation ou NSBlockOperation
  • Exemplo -(IBAction)carregaLista:(UIButton *) botao { NSOperationQueue * queue = [[NSOperationQueue alloc]init]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget: self selector: @selector(downloadMotos) object:nil]; ! [queue addOperation:operation]; } ! -(void)downloadPosts{ //faz a comunicao com algum webservice! [self performSelectorOnMainThread @selector(atualizaTabela:) withObject: arrayDeDados waitUntilDone: NO]; ! } ! -(void)atualizaTabela:(NSObject*) object{ //... [self.tableView reloadData]; }
  • Opo 3 Grand Central Dispatch (GCD)
  • Exemplo NSArray *images = @[@"http://example.com/image1.png", @"http://example.com/image2.png", @"http://example.com/image3.png", @"http://example.com/image4.png"]; ! dispatch_queue_t imageQueue = dispatch_queue_create("Image Queue",NULL); ! for (NSString *urlString in images) { dispatch_async(imageQueue, ^{ NSURL *url = [NSURL URLWithString:urlString]; NSData *imageData = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:imageData]; ! NSUInteger imageIndex = [images indexOfObject:urlString]; UIImageView *imageVIew = (UIImageView *)[self.view viewWithTag:imageIndex]; if (!imageView) return; dispatch_async(dispatch_get_main_queue(), ^{ // Update the UI [imageVIew setImage:image]; }); }); }
  • Conexo com webservices
  • Opo 1 NSURLRequest e NSURLMutableRequest NSURLConnection
  • Exemplo(GET) NSString * url = @http://projetows.heroku.com/motos.json ! NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString: url] ]; ! [NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) { // manipula resposta } ];
  • Opo 2 ! NSURLSession (iOS 7)
  • Exemplo (GET) NSString * url = @http://projetows.heroku.com/motos.json ! NSURLSession *session = [NSURLSession sharedSession]; [session dataTaskWithURL: [NSURL URLWithString: url]completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //manipula resposta! } ] resume];
  • Interpretando resultados
  • JSON projetows.heroku.com/motos.json [ { "marca":"Yamaha", "modelo":"R1", }, { "marca":"Honda", modelo":"CBR 600 RR", } ]
  • Opo da Apple NSJSONSerialization https://developer.apple.com/library/ios/documentation/ foundation/reference/ nsjsonserialization_class
  • Exemplo NSString *url = @http://projetows.heroku.com/motos.json";! ! NSData *jsonData = [NSData dataWithContentsOfURL: [NSURL URLWithString:url]];! ! NSError *error;! NSArray *resultados = [NSJSONSerialization JSONObjectWithData: jsonData options:NSJSONReadingMutableContainers error:&error];! if(!error) {! ! for(NSDictionary * moto in resultados){! ! ! NSString *marca = [mot