The Importance of
   Reachability
         Marc Weil
      CTO, CloudMine
     marc@cloudmine.me
Wireless connections aren’t perfect
Defensive Programming
Let’s look at a quick example
- (void)doLoad {
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init];
    NSData *responseBody = [NSURLConnection sendSynchronousRequest:req
                                                  returningResponse:&response
                                                              error:NULL];
    // responseBody contains the response body
}



                                    Why is this naive?




• Potentially blocks UI thread
• No error handling
• What happens if there’s no network connection?
- (void)doLoad {
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
    [NSURLConnection sendAsynchronousRequest:req
                                        queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *resp, NSData *body, NSError *err) {
                               if (!err) {
                                   NSLog(@"Success!");
                                   // body argument contains response body
                               } else {
                                   NSLog(@"Error!");
                               }
                           }];
}



                                Why is this still naive?



• Potentially blocks UI thread
• No error handling
• What happens if there’s no network connection?
- (void)doLoad {
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
    [NSURLConnection sendAsynchronousRequest:req
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *resp, NSData *body, NSError *err) {
                               if (!err) {
                                   NSLog(@"Success!");
                                   // body argument contains response body
                               } else {
                                   NSLog(@"Error!");
                               }
                           }];
}



                                Why is this still naive?



• Potentially blocks UI thread
• No error handling
• What happens if there’s no network connection?
Queues are your best friend
Internet
When everything is dandy...
                                              HT
                                                TP




                                          )
                                               HTTP




                                      eue
                                   qu
                                               HTTP




                                   e
                                th
                                                        Constant




                              is
                                               HTTP




                              is
                                                          flow


                          (th
                                               HTTP
                                               HTTP
                                               HTTP
        Request
       generator          HTTP                 HTTP
Internet
When everything is not so
       dandy...

                                             HTTP
                                             HTTP




                                         )
                                             HTTP




                                      ue
                                      e
                                   qu
                                             HTTP




                                  e
                               th
                                                    Halted




                              is
                                             HTTP




                             is
                                                     flow


                        (th
                                             HTTP
                                             HTTP
                                             HTTP
       Request
      generator             HTTP             HTTP
How can I do this in code?
      (apologies to Android devs)
Three components:

1. NSOperationQueue
2. NSOperation
3. Reachability
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{

      NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
      NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init];
      NSData *responseBody = [NSURLConnection sendSynchronousRequest:req
                                                   returningResponse:&response
                                                               error:NULL];
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{

      NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
      NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init];
      NSData *responseBody = [NSURLConnection sendSynchronousRequest:req
                                                   returningResponse:&response
                                                               error:NULL];
}];




      This is a convenience method that creates an
         NSBlockOperation under the hood.
Internet


                                  HT
                                    TP




                             ue
                           ue
                         nQ
                       io
                     at
                   er
                 Op
                                   HTTP




               NS
                                   HTTP
                                               Constant
                                   HTTP
                                                 flow
            NSOperation            HTTP    (NSOperations)
                                   HTTP
                                   HTTP
 Request
generator         HTTP             HTTP
What about Reachability?
Everything is contained in
SystemConfiguration.framework
But don’t use that directly... it’s terrible
https://github.com/tonymillion/Reachability

          (love abstraction!)
// allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableOnWWAN = YES;

// set the blocks
reach.reachableBlock = ^(Reachability *reach)
{
    queue.suspended = NO;
};

reach.unreachableBlock = ^(Reachability *reach)
{
    queue.suspended = YES;
};

// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];




             (example from readme on GitHub)
!
                                    POW




    Bringing out the big guns...
https://github.com/AFNetworking/AFNetworking




                                               (th
                                                an
                                                   k sG
                                                       ow
                                                          all
                                                              a!!
                                                                <3!
                                                                      )
One more pet peeve....
 are apps that don’t do this
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
Resources
https://github.com/tonymillion/Reachability
https://github.com/AFNetworking/AFNetworking

http://developer.apple.com/library/ios/#samplecode/Reachability/
Introduction/Intro.html




Contact Me
e: marc@cloudmine.me
t: @marcweil

Reachability in Mobile App Development

  • 1.
    The Importance of Reachability Marc Weil CTO, CloudMine marc@cloudmine.me
  • 2.
  • 3.
  • 4.
    Let’s look ata quick example
  • 5.
    - (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL]; // responseBody contains the response body } Why is this naive? • Potentially blocks UI thread • No error handling • What happens if there’s no network connection?
  • 6.
    - (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *body, NSError *err) { if (!err) { NSLog(@"Success!"); // body argument contains response body } else { NSLog(@"Error!"); } }]; } Why is this still naive? • Potentially blocks UI thread • No error handling • What happens if there’s no network connection?
  • 7.
    - (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *body, NSError *err) { if (!err) { NSLog(@"Success!"); // body argument contains response body } else { NSLog(@"Error!"); } }]; } Why is this still naive? • Potentially blocks UI thread • No error handling • What happens if there’s no network connection?
  • 8.
    Queues are yourbest friend
  • 9.
    Internet When everything isdandy... HT TP ) HTTP eue qu HTTP e th Constant is HTTP is flow (th HTTP HTTP HTTP Request generator HTTP HTTP
  • 10.
    Internet When everything isnot so dandy... HTTP HTTP ) HTTP ue e qu HTTP e th Halted is HTTP is flow (th HTTP HTTP HTTP Request generator HTTP HTTP
  • 11.
    How can Ido this in code? (apologies to Android devs)
  • 12.
    Three components: 1. NSOperationQueue 2.NSOperation 3. Reachability
  • 13.
    NSOperationQueue *queue =[[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL]; }];
  • 14.
    NSOperationQueue *queue =[[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL]; }]; This is a convenience method that creates an NSBlockOperation under the hood.
  • 15.
    Internet HT TP ue ue nQ io at er Op HTTP NS HTTP Constant HTTP flow NSOperation HTTP (NSOperations) HTTP HTTP Request generator HTTP HTTP
  • 16.
  • 17.
    Everything is containedin SystemConfiguration.framework
  • 18.
    But don’t usethat directly... it’s terrible
  • 19.
  • 20.
    // allocate areachability object Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"]; reach.reachableOnWWAN = YES; // set the blocks reach.reachableBlock = ^(Reachability *reach) { queue.suspended = NO; }; reach.unreachableBlock = ^(Reachability *reach) { queue.suspended = YES; }; // start the notifier which will cause the reachability object to retain itself! [reach startNotifier]; (example from readme on GitHub)
  • 21.
    ! POW Bringing out the big guns... https://github.com/AFNetworking/AFNetworking (th an k sG ow all a!! <3! )
  • 22.
    One more petpeeve.... are apps that don’t do this [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  • 23.