Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Working with APIs on iOS

Working with APIs on iOS

This is a presentation I gave to the University of Houston (Downtown) ACM Student Chapter on March 4th.

The topic itself was chosen by the student chapter as a general introduction on the different aspects of working with remote web service APIs on the iOS platform.

João Prado Maia

March 04, 2015
Tweet

More Decks by João Prado Maia

Other Decks in Programming

Transcript

  1. Working with APIs on iOS João Prado Maia, Ipanema Labs

    LLC March 4th 2015 © Ipanema Labs LLC, 2015 1
  2. Me • Building apps since 2008 • Freelancing full time

    since 2011 • Principal at Ipanema Labs LLC • Full time freelance iOS developer out of The Woodlands © Ipanema Labs LLC, 2015 2
  3. Meetups • Founder/Organizer of Houston iPhone Developers Meetup • Organizer

    of Houston PHP Users Group since 2005 © Ipanema Labs LLC, 2015 3
  4. Knee Cap • Early 2009 • First app on store,

    simple SQLite structure © Ipanema Labs LLC, 2015 4
  5. HAR (client) • Early 2010 on iPhone, 2011 as Universal

    app • Single developer on this application • Really big and complex application, mirroring features available on HAR.com © Ipanema Labs LLC, 2015 5
  6. Houston Living (client) • Mid 2013 • Single developer on

    this application • Universal app from the start © Ipanema Labs LLC, 2015 6
  7. New HAR (client) • Launching in a couple of months

    • Single developer on this application • Complete redesign of the app (and website) © Ipanema Labs LLC, 2015 7
  8. Working with APIs on iOS • Parsing/generating JSON • Old

    school options like TouchJSON, JSONKit or json- framework. • NSJSONSerialization from Apple. © Ipanema Labs LLC, 2015 8
  9. Example JSON document { "title": null, "results": ["Joe", "Bob", "Mary",

    "Julia"], "total": 4, "more_content": true } © Ipanema Labs LLC, 2015 9
  10. Parsing JSON (1/2) NSString *jsonDocumentStr = @""; NSStringEncoding encoding =

    NSUTF8StringEncoding; NSData *data = [jsonDocumentStr dataUsingEncoding:encoding]; NSError *error = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; © Ipanema Labs LLC, 2015 10
  11. Parsing JSON (2/2) NSString *title = [jsonObject objectForKey:@"title"]; NSArray *names

    = [jsonObject objectForKey:@"results"]; for (NSString *name in names) { NSLog(@"name = %@", name); } BOOL moreContent = [[jsonObject objectForKey:@"more_content"] boolValue]; © Ipanema Labs LLC, 2015 11
  12. Generating JSON • Simple to generate JSON from objects. NSArray

    *names = @[@"Joe", "Bob", "Mary"]; NSError *error = nil; NSData *data = [NSJSONSerialization dataWithJSONObject:names options:0 error:&error]; NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"jsonStr = %@", jsonStr); • The output will be: ["Joe", "Bob", "Mary"] © Ipanema Labs LLC, 2015 12
  13. Networking • Most of the time you will be using

    a remote API for your applications. • Options: • NSURLConnection (standard) • NSURLSession (newer, also standard) • AFNetworking (third party, extremely popular) © Ipanema Labs LLC, 2015 13
  14. NSURLConnection example (1/3) @interface ViewController () @property (nonatomic, strong) NSMutableData

    *responseData; @end @implementation ViewController - (void)loadJsonContent { NSURL *url = [NSURL URLWithString:@"http://ipanemalabs.com/example.json"]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; } © Ipanema Labs LLC, 2015 14
  15. NSURLConnection example (2/3) #pragma mark NSURLConnectionDelegate methods - (void)connection:(NSURLConnection *)connection

    didReceiveResponse:(NSURLResponse *)response { self.responseData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.responseData appendData:data]; } © Ipanema Labs LLC, 2015 15
  16. NSURLConnection example (3/3) - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSError *error =

    nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error]; NSLog(@"jsonObject = %@", jsonObject); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { // TODO: show error message } © Ipanema Labs LLC, 2015 16
  17. AFNetworking example AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:@"http://ipanemalabs.com/example.json" parameters:nil

    success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; © Ipanema Labs LLC, 2015 17
  18. AFNetworking • CocoaPods support. • Great documentation available from CocoaDocs

    • Lots of tutorials/articles available on the web about this library. © Ipanema Labs LLC, 2015 18
  19. Realistic AFNetworking example • Create a subclass of AFHTTPSessionManager. •

    Business logic related to your API goes in there. • Things like API keys, authentication, etc. • NSScreencast has a great episode on AFNetworking that goes over the details. • Even source code available for free on the URL above. © Ipanema Labs LLC, 2015 19
  20. Tips / Links • JSONLint (free) is a great tool

    to validate JSON, and to get a readable version from a jumbled up auto-generated version. • HTTP Client ($1.99) is a native Mac application that makes it easy to build HTTP requests to APIs, and inspect the results. © Ipanema Labs LLC, 2015 20
  21. Contact • João Prado Maia - [email protected] • Reach me

    at (936) 718-1376 • Website at http://ipanemalabs.com • Twitter: @joaopmaia © Ipanema Labs LLC, 2015 21