Created
December 6, 2012 05:45
-
-
Save howanghk/4222055 to your computer and use it in GitHub Desktop.
check internet connection with NSURLConnection and Google in response to http://stackoverflow.com/a/13736429/613541
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
NSString *urlString = @"http://www.google.com/"; | |
NSURL *url = [NSURL URLWithString:urlString]; | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
[request setHTTPMethod:@"HEAD"]; | |
NSHTTPURLResponse *response; | |
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL]; | |
if ([response statusCode] == 200) | |
{ | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
// Do anything you would like to do as if you have Internet connection | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Google server return HTTP Status Code 200 OK" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alertView show]; | |
}); | |
} | |
else | |
{ | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
// Do anything you would like to do as if you cannot see Google homepage | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Connect to Google failed or Google server did not return HTTP Status Code 200" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alertView show]; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment