conceitos e prática no desenvolvimento ios - mobile conf 2014
DESCRIPTION
Slides da minha palestra na Mobile Conf 2014.TRANSCRIPT
Conceitos e prática no desenvolvimento iOS
Desenvolvedor e instrutor (iOS, Android, Java e Ruby)
Quem sou eu?
@fabiopimentelgithub.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
Opções para View
StoryBoard
Xib
Opções para View
1) StoryBoard2) Xib2) Programaticamente
Ciclo de Vida
ViewController
A tela já existe?
ViewController
A tela já existe?
viewWillAppear
Não
viewDidAppear
Tela visível
viewDidLoadFluxo
ViewController
A tela já existe?
Sim
viewWillAppear
Não
viewDidAppear
Tela visível
viewDidLoad
viewDidLoad
viewWillAppear
Tela visível
viewWillDisappear
Ciclo de vida - ViewController
viewDidAppear
viewDidDisappear
Tela não visível
Ciclo de Vida - App
Aplicação não está rodando
Usuário clica no ícone
da appapplication:didFinishLauchingWithOptions:
applicationDidBecomeActive:
Aplicação rodando
Usuário clica em Home, ou recebe
chamada telefônica/SMS
applicationWillResignActive:
applicationDidEnterBackground:
Aplicação em backgroung
Usuário clica no ícone
da appapplicationWillEnterForeground:
Ciclo de Vida - App
Aplicação não está rodando
Usuário clica no ícone
da appapplication:didFinishLauchingWithOptions:
applicationDidBecomeActive:
Aplicação rodando
Usuário clica em Home, ou recebe
chamada telefônica/SMS
applicationWillResignActive:
applicationDidEnterBackground:
Aplicação em backgroung
Usuário clica no ícone
da appapplicationWillEnterForeground:
Live Coding
First
Roun
d
Principal componente do iOS
TableViewhttps://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 identification of rows and sections in UITableView objects and the identification 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
UITableViewControllerhttps://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewController_Class/
Reference/Reference.html
Métodos para configuração
– numberOfSectionsInTableView:
- tableView:numberOfRowsInSection:
– tableView:cellForRowAtIndexPath:
Trabalhando em Background
Opção 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 comunicação 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 comunicação com algum webservice
[self performSelectorOnMainThread @selector(atualizaTabela:) withObject: nil waitUntilDone: NO];!
}!
-(void)atualizaTabela:(NSObject*) object!
{
! ! [self.tableView reloadData];!
}
Opção 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 comunicação com algum webservice! [self performSelectorOnMainThread @selector(atualizaTabela:) withObject: arrayDeDados waitUntilDone: NO]; !} !-(void)atualizaTabela:(NSObject*) object{ //... [self.tableView reloadData]; }
Opção 3
Grand Central Dispatch (GCD)
ExemploNSArray *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]; }); }); }
Conexão com webservices
Opção 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 } ];
Opção 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
JSONprojetows.heroku.com/motos.json
[ { "marca":"Yamaha", "modelo":"R1", }, { "marca":"Honda", “modelo":"CBR 600 RR", } ]
Opção da Apple
NSJSONSerializationhttps://developer.apple.com/library/ios/documentation/ foundation/reference/nsjsonserialization_class
ExemploNSString *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 = [moto objectForKey:@"marca"];! ! ! NSString *modelo = [moto objectForKey:@"modelo"];! ! ! NSLog(@"Marca: %@, modelo: %@", marca, modelo);!! }!}!
Opções de terceiros
https://github.com/stig/json-framework/JSON Framework
https://github.com/TouchCode/TouchJSONTouchJSON
Agora basta juntar tudo?
https://github.com/AFNetworking/AFNetworking
Exemplo!NSString * url = @“projetows.heroku.com/motos.json”!AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];!![manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {!! !! self.json = responseObject;! ! for(NSDictionary * moto in self.json){! ! ! NSString *marca = [moto objectForKey:@"marca"];! ! ! NSString *modelo = [moto objectForKey:@"modelo"];! ! ! NSLog(@"Marca: %@, modelo: %@", marca, modelo);! ! }! } failure:^(AFHTTPRequestOperation *operation, NSError *error) {! NSLog(@"Error: %@", error);! ! }];!}!
Como usá-lo no projeto?
Instalação
gem install cocoapods
Instalação
gem install cocoa-pods
Podfile
Podfile
platform :ios , '7.1'!!
pod 'AFNetworking', '> 2.0'
Instalaçãogem install cocoa-pods
Podfile
pod install
Saída(Terminal)Fabios-MacBook-Pro:MobileConf fabiopimentel$ pod install Analyzing dependencies Downloading dependencies Installing AFNetworking (2.2.4) Generating Pods project Integrating client project ![!] From now on use `MobileConf.xcworkspace`.
Estrutura do projeto
Live Coding
Second
Round
Mais bibliotecas …
TestesKiwi
https://github.com/kiwi-bdd/KiwiBDD
style
describe(@"Team", ^{ context(@"when newly created", ^{ it(@"should have a name", ^{ id team = [Team team]; [[team.name should] equal:@"Black Hawks"]; }); ! it(@"should have 11 players", ^{ id team = [Team team]; [[[team should] have:11] players]; }); }); });
Testes
KIFhttps://github.com/kif-framework/KIF
Acceptance Tests
Kiwihttps://github.com/kiwi-bdd/Kiwi
BDD style
KIF Acceptance Tests@implementation LoginTests !- (void)beforeEach { [tester navigateToLoginPage]; } !- (void)afterEach { [tester returnToLoggedOutHomeScreen]; } !- (void)testSuccessfulLogin { [tester enterText:@"[email protected]" intoViewWithAccessibilityLabel:@"Login User Name"]; [tester enterText:@"thisismypassword" intoViewWithAccessibilityLabel:@"Login Password"]; [tester tapViewWithAccessibilityLabel:@"Log In"]; ! // Verify that the login succeeded [tester waitForTappableViewWithAccessibilityLabel:@"Welcome"]; } !@end
Ainda mais…
https://www.testflightapp.com/
AppCode (IDE)
E agora?
http://www.caelum.com.br/curso-ios-iphone-ipad/
https://developer.apple.com/programs/ios/
Obrigado!@fabiopimentel