Upload
makoto-miyagawa
View
96
Download
2
Embed Size (px)
DESCRIPTION
Apresentação feita na UFAM dia 09/09/2014 demonstrando como consumir webservices REST em iOS através da biblioteca AFNetworking. O exemplo utilizado foi de lista de tarefas.
Citation preview
Fazendo uma aplicação cliente/servidor (e algumas dicas…)
Makoto [email protected]
! github.com/makotomiy
Bemol
Principais projetos
Pré-venda Móvel (2012)
• Sistema de orçamento móvel no iPod
• Agilidade no atendimento ao cliente
Principais projetos
Quiosque (2013)
• Terminal de auto-atendimento no iPad
• Consulta de informações e pagamento de prestações
O que eles têm em comum?
Modelo cliente/servidor
Proposta: Webserver + Client
Protocolo REST
Métodos HTTP !
GET POST PUT
DELETE
CRUD !
Create (INSERT) Read (SELECT)
Update (UPDATE) Destroy (DELETE)
Protocolo REST
GET /clientes => SELECT * FROM clientes GET /clientes/1 => SELECT * FROM clientes WHERE id = 1 POST /clientes => INSERT INTO clientes VALUES … PUT /clientes/1 => UPDATE clientes SET … WHERE id = 1
Serialização e mapeamento
{ “cliente”: { “nome” : “João”, “idade” : 30, “casado” : true } }
@interface Cliente : NSObject { NSString *nome; NSNumber *idade; NSNumber *casado; } @end
Checklist
" Chrome: REST Console
# Ruby
" Xcode (+ Command line tools)
http://goo.gl/YHvJtG
REST Console
RubyGems e Bundler
RubyGems e Bundler
$ sudo gem install bundler $ cd server $ bundle install
DataMapper
Object Relational Mapper
SELECT * FROM cliente =
Cliente.all()
Object Relational Mapper
INSERT INTO cliente VALUES … =
Cliente.create(:nome => …)
Criar tabela
class Tarefa include DataMapper::Resource ! property :id, Serial, :key => true property :titulo, String, :required => true property :descricao, String property :concluido, Boolean, :default => false end
database.rb
Webserver: Hello World
get '/' do 'Hello World!' end
app.rb
Webserver: GET /tarefas
get '/tarefas' do content_type :json ! tarefas = Tarefa.all.collect {|t| t.attributes} ! tarefas.to_json end
app.rb
Webserver: POST /tarefas
post '/tarefas' do content_type :json ! conteudo = JSON.parse(request.body.read) nova_tarefa = Tarefa.create(conteudo) ! nova_tarefa.attributes.to_json end
app.rb
Cocoapods
Cocoapods
$ sudo gem install cocoapods $ cd client/Todo\ List $ pod init $ pod install
AFNetworking
AFNetworking
{ “cliente”: { “nome” : “João”, “idade” : 30, “casado” : true } }
@{ @“cliente”: @{ @“nome” : @“João”, @“idade” : @30, @“casado” : @YES } }
Client: To-do list
Client: To-do list
@interface TDTarefa : NSObject !@property (nonatomic, strong) NSNumber *idTarefa; @property (nonatomic, strong) NSString *atividade; @property (nonatomic, strong) NSNumber *concluido; !- (BOOL)isConcluido; - (instancetype)initFromDictionary:(NSDictionary *)dict; -(NSDictionary *)dictionaryRepresentation; !@end
TDTarefa.h
Client: To-do list
@protocol TDTodoListDelegate <NSObject> - (void)todoListGetListaSuccess:(NSArray *)array; - (void)todoListRequestFailed:(NSError *)error; @end !@interface TDTodoList : NSObject !@property (nonatomic, weak) id<TDTodoListDelegate> __weak delegate; !- (void)buscarLista; @end
TDTodoList.h
Client: To-do list
- (void)buscarLista { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; ! [manager GET: parameters: success: failure:]; }
TDTodoList.m
Client: To-do list
- (void)criarTarefa:(TDTarefa *)tarefa { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; ! [manager POST: parameters: success: failure:]; }
TDTodoList.m
Client: To-do list
- (void)atualizarTarefa:(TDTarefa *)tarefa { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager PUT: parameters: success: failure:]; }
TDTodoList.m
E as dicas?
Debugging
- breakpoint: All Exceptions - breakpoint condicional - breakpoints com ação - lldb: p e po
Cocoapods
Alcatraz
Alcatraz
KSImageNamed
Alcatraz
OMColorSense
CocoaControls
RestKit
Heroku
NSHipster
iOS Dev Weekly
objc.io
Dúvidas?
Obrigado!
[email protected] ! github.com/makotomiy