Conceitos e prática 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
Opções para View
StoryBoard
Xib
Opções para View
1) StoryBoard
2) Xib
2) Programaticamente
Ciclo de Vida
ViewController
A tela já
existe?
ViewController
A tela já
existe?
viewWillAppear
Não
viewDidAppear
Tela visível
viewDidLoad
F
l
u
x
o
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
Te...
Ciclo de Vida - App
Aplicação
não está
rodando
Usuário
clica no ícone
da app
application:didFinishLauchingWithOptions:
app...
Ciclo de Vida - App
Aplicação
não está
rodando
Usuário
clica no ícone
da app
application:didFinishLauchingWithOptions:
app...
Live Coding
First
Round
Principal componente do iOS
TableView
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/
AboutTableView...
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 facil...
NSIndexPath
Row Section
Row 2
Section 2
NSIndexPath
UITableViewController
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewController_Class/
R...
Métodos para configuração
– numberOfSectionsInTableView:

- tableView:numberOfRowsInSection:

– tableView:cellForRowAtIndex...
Trabalhando em Background
Opção 1
performSelectorInBackground:withObject:

performSelectonOnMainThread:withObject:
waitUntilDone
Exemplo
-(IBAction)carregaLista:(UIButton *) botao !
{

! [self performSelectorInBackground: @selector(buscaMotos) withObj...
Exemplo
-(IBAction)carregaLista:(UIButton *) botao !
{

! [self performSelectorInBackground: @selector(buscaMotos) withObj...
Exemplo
-(IBAction)carregaLista:(UIButton *) botao !
{

! [self performSelectorInBackground: @selector(buscaMotos) withObj...
Opção 2
NSOperationQueue

NSInvocationOperation ou NSBlockOperation
Exemplo
-(IBAction)carregaLista:(UIButton *) botao {

NSOperationQueue * queue = [[NSOperationQueue alloc]init];

NSInvoca...
Opção 3
Grand Central Dispatch (GCD)
Exemplo
NSArray *images = @[@"http://example.com/image1.png",

@"http://example.com/image2.png",

@"http://example.com/ima...
Conexão com
webservices
Opção 1
NSURLRequest e
NSURLMutableRequest

NSURLConnection
Exemplo(GET)
NSString * url = @“http://projetows.heroku.com/motos.json”

!
NSURLRequest *request = [NSURLRequest requestWi...
Opção 2
!
NSURLSession (iOS 7)
Exemplo (GET)
NSString * url = @“http://projetows.heroku.com/motos.json”

!
NSURLSession *session = [NSURLSession sharedSe...
Interpretando
resultados
JSON
projetows.heroku.com/motos.json
[
{
"marca":"Yamaha",
"modelo":"R1",
},
{
"marca":"Honda",
“modelo":"CBR 600 RR",
}
]
Opção da Apple
NSJSONSerialization
https://developer.apple.com/library/ios/documentation/ foundation/reference/
nsjsonseri...
Exemplo
NSString *url = @“http://projetows.heroku.com/motos.json";!
!
NSData *jsonData = [NSData dataWithContentsOfURL: [N...
Opções de terceiros
https://github.com/stig/json-framework/
JSON Framework
https://github.com/TouchCode/TouchJSON
TouchJSON
Agora basta juntar
tudo?
https://github.com/AFNetworking/
AFNetworking
Exemplo
!
NSString * url = @“projetows.heroku.com/motos.json”!
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOper...
Como usá-lo no projeto?
CocoaPods
http://cocoapods.org/
Gerenciador de
dependências
Instalação
gem install cocoapods
Instalação
gem install cocoa-pods
Podfile
Podfile
platform :ios , '7.1'!
!
pod 'AFNetworking', '> 2.0'
Instalação
gem install cocoa-pods
Podfile
pod install
Saída(Terminal)
Fabios-MacBook-Pro:MobileConf fabiopimentel$ pod install

Analyzing dependencies

Downloading dependencies...
Estrutura
do projeto
Live Coding
econd
Round
Mais bibliotecas …
Testes
Kiwi
https://github.com/kiwi-bdd/Kiwi
BDD
style
describe(@"Team", ^{

context(@"when newly created", ^{

it(@"shoul...
Testes
KIF
https://github.com/kif-framework/KIF
Acceptance Tests
Kiwi
https://github.com/kiwi-bdd/Kiwi
BDD style
KIF Acceptance Tests
@implementation LoginTests

!
- (void)beforeEach

{

[tester navigateToLoginPage];

}

!
- (void)afte...
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
Upcoming SlideShare
Loading in …5
×

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

1,591 views

Published on

Slides da minha palestra na Mobile Conf 2014.

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,591
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
35
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

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

  1. 1. Conceitos e prática no desenvolvimento iOS
  2. 2. Desenvolvedor e instrutor (iOS, Android, Java e Ruby) Quem sou eu? @fabiopimentel github.com/fabiopimentel
  3. 3. Vamos programar? Objective-C (OFICIAL) Ruby - RubyMotion C# - Xamarin
  4. 4. iOS MVC Controller Model View
  5. 5. iOS MVC Controller Model View -------------- --------------
  6. 6. iOS MVC Controller Model View -------------- --------------
  7. 7. iOS MVC Controller Model View -------------- -------------- Outlet
  8. 8. iOS MVC Controller Model View -------------- -------------- Outlet Action
  9. 9. Opções para View
  10. 10. StoryBoard
  11. 11. Xib
  12. 12. Opções para View 1) StoryBoard 2) Xib 2) Programaticamente
  13. 13. Ciclo de Vida
  14. 14. ViewController A tela já existe?
  15. 15. ViewController A tela já existe? viewWillAppear Não viewDidAppear Tela visível viewDidLoad F l u x o
  16. 16. ViewController A tela já existe? Sim viewWillAppear Não viewDidAppear Tela visível viewDidLoad
  17. 17. viewDidLoad viewWillAppear Tela visível viewWillDisappear Ciclo de vida - ViewController viewDidAppear viewDidDisappear Tela não visível
  18. 18. Ciclo de Vida - App Aplicação não está rodando Usuário clica no ícone da app application: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 app applicationWillEnterForeground:
  19. 19. Ciclo de Vida - App Aplicação não está rodando Usuário clica no ícone da app application: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 app applicationWillEnterForeground:
  20. 20. Live Coding First Round
  21. 21. Principal componente do iOS
  22. 22. TableView https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ AboutTableViewsiPhone/AboutTableViewsiPhone.html
  23. 23. UITableViewStyleGrouped
  24. 24. UITableViewStylePlain
  25. 25. Section 0 Section 1 Section 2 Section 3 Section 4
  26. 26. Row 0 Row 1
  27. 27. Row 0 Row 1 Row 2
  28. 28. 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
  29. 29. NSIndexPath Row Section
  30. 30. Row 2 Section 2 NSIndexPath
  31. 31. UITableViewController https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewController_Class/ Reference/Reference.html
  32. 32. Métodos para configuração – numberOfSectionsInTableView: - tableView:numberOfRowsInSection: – tableView:cellForRowAtIndexPath:
  33. 33. Trabalhando em Background
  34. 34. Opção 1 performSelectorInBackground:withObject: performSelectonOnMainThread:withObject: waitUntilDone
  35. 35. Exemplo -(IBAction)carregaLista:(UIButton *) botao ! {
 ! [self performSelectorInBackground: @selector(buscaMotos) withObject: nil];! }
 

  36. 36. 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];! }
  37. 37. 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];! }
  38. 38. Opção 2 NSOperationQueue NSInvocationOperation ou NSBlockOperation
  39. 39. 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]; }
  40. 40. Opção 3 Grand Central Dispatch (GCD)
  41. 41. 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]; }); }); }
  42. 42. Conexão com webservices
  43. 43. Opção 1 NSURLRequest e NSURLMutableRequest NSURLConnection
  44. 44. 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 } ];
  45. 45. Opção 2 ! NSURLSession (iOS 7)
  46. 46. 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];
  47. 47. Interpretando resultados
  48. 48. JSON projetows.heroku.com/motos.json [ { "marca":"Yamaha", "modelo":"R1", }, { "marca":"Honda", “modelo":"CBR 600 RR", } ]
  49. 49. Opção da Apple NSJSONSerialization https://developer.apple.com/library/ios/documentation/ foundation/reference/ nsjsonserialization_class
  50. 50. 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 = [moto objectForKey:@"marca"];! ! ! NSString *modelo = [moto objectForKey:@"modelo"];! ! ! NSLog(@"Marca: %@, modelo: %@", marca, modelo);! ! }! }!
  51. 51. Opções de terceiros https://github.com/stig/json-framework/ JSON Framework https://github.com/TouchCode/TouchJSON TouchJSON
  52. 52. Agora basta juntar tudo?
  53. 53. https://github.com/AFNetworking/ AFNetworking
  54. 54. 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);! ! }];! }!
  55. 55. Como usá-lo no projeto?
  56. 56. CocoaPods http://cocoapods.org/ Gerenciador de dependências
  57. 57. Instalação gem install cocoapods
  58. 58. Instalação gem install cocoa-pods Podfile
  59. 59. Podfile platform :ios , '7.1'! ! pod 'AFNetworking', '> 2.0'
  60. 60. Instalação gem install cocoa-pods Podfile pod install
  61. 61. 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`.
  62. 62. Estrutura do projeto
  63. 63. Live Coding econd Round
  64. 64. Mais bibliotecas …
  65. 65. Testes Kiwi https://github.com/kiwi-bdd/Kiwi BDD 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]; }); }); });
  66. 66. Testes KIF https://github.com/kif-framework/KIF Acceptance Tests Kiwi https://github.com/kiwi-bdd/Kiwi BDD style
  67. 67. KIF Acceptance Tests @implementation LoginTests ! - (void)beforeEach { [tester navigateToLoginPage]; } ! - (void)afterEach { [tester returnToLoggedOutHomeScreen]; } ! - (void)testSuccessfulLogin { [tester enterText:@"user@example.com" intoViewWithAccessibilityLabel:@"Login User Name"]; [tester enterText:@"thisismypassword" intoViewWithAccessibilityLabel:@"Login Password"]; [tester tapViewWithAccessibilityLabel:@"Log In"]; ! // Verify that the login succeeded [tester waitForTappableViewWithAccessibilityLabel:@"Welcome"]; } ! @end
  68. 68. Ainda mais…
  69. 69. https://www.testflightapp.com/
  70. 70. AppCode (IDE)
  71. 71. E agora?
  72. 72. http://www.caelum.com.br/curso-ios-iphone-ipad/
  73. 73. https://developer.apple.com/programs/ios/
  74. 74. Obrigado! @fabiopimentel

×