Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (BOOL)isInternetOk
- {
- Reachability *curReach = [Reachability reachabilityWithHostName:@"apple.com"];
- NetworkStatus netStatus = [curReach currentReachabilityStatus];
- if (netStatus != NotReachable) //if internet connexion ok
- {
- return YES;
- }
- else
- {
- return NO;
- }
- }
- - (BOOL)isInternetOk2
- {
- NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
- NSURL* URL = [NSURL URLWithString:@"https://www.google.com"];
- NSError *error = nil;
- [request setURL:URL];
- [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
- [request setTimeoutInterval:15];
- NSData* response2 = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
- if (error)
- {
- return NO;
- }
- else
- {
- return YES;
- }
- }
- NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]];
- request.timeoutInterval = 10;
- [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
- {
- NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
- NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description);
- if ((long)[httpResponse statusCode] >= 200 && (long)[httpResponse statusCode]< 400)
- {
- // do stuff
- NSLog(@"Connected!");
- }
- else
- {
- NSLog(@"Not connected!");
- }
- }];
- +(bool)isNetworkAvailable {
- SCNetworkReachabilityFlags flags;
- SCNetworkReachabilityRef address;
- address = SCNetworkReachabilityCreateWithName(NULL, "www.apple.com");
- Boolean success = SCNetworkReachabilityGetFlags(address, &flags);
- CFRelease(address);
- bool canReach = success
- && !(flags & kSCNetworkReachabilityFlagsConnectionRequired)
- && (flags & kSCNetworkReachabilityFlagsReachable);
- return canReach;
- }
Add Comment
Please, Sign In to add comment