More Related Content Similar to Reachability in Mobile App Development (20) Reachability in Mobile App Development5. - (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?
9. 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
10. 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
11. How can I do this in code?
(apologies to Android devs)
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
20. // 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)
21. !
POW
Bringing out the big guns...
https://github.com/AFNetworking/AFNetworking
(th
an
k sG
ow
all
a!!
<3!
)
22. One more pet peeve....
are apps that don’t do this
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];