Guest User

Untitled

a guest
Jan 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. - (BOOL)isInternetOk
  2. {
  3. Reachability *curReach = [Reachability reachabilityWithHostName:@"apple.com"];
  4. NetworkStatus netStatus = [curReach currentReachabilityStatus];
  5.  
  6. if (netStatus != NotReachable) //if internet connexion ok
  7. {
  8. return YES;
  9. }
  10. else
  11. {
  12. return NO;
  13. }
  14. }
  15.  
  16. - (BOOL)isInternetOk2
  17. {
  18. NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
  19. NSURL* URL = [NSURL URLWithString:@"https://www.google.com"];
  20. NSError *error = nil;
  21.  
  22. [request setURL:URL];
  23. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  24. [request setTimeoutInterval:15];
  25.  
  26. NSData* response2 = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
  27. if (error)
  28. {
  29. return NO;
  30. }
  31. else
  32. {
  33. return YES;
  34. }
  35. }
  36.  
  37. NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
  38. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]];
  39. request.timeoutInterval = 10;
  40.  
  41. [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
  42. {
  43. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  44. NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description);
  45.  
  46. if ((long)[httpResponse statusCode] >= 200 && (long)[httpResponse statusCode]< 400)
  47. {
  48. // do stuff
  49. NSLog(@"Connected!");
  50. }
  51. else
  52. {
  53. NSLog(@"Not connected!");
  54. }
  55. }];
  56.  
  57. +(bool)isNetworkAvailable {
  58. SCNetworkReachabilityFlags flags;
  59. SCNetworkReachabilityRef address;
  60. address = SCNetworkReachabilityCreateWithName(NULL, "www.apple.com");
  61. Boolean success = SCNetworkReachabilityGetFlags(address, &flags);
  62. CFRelease(address);
  63. bool canReach = success
  64. && !(flags & kSCNetworkReachabilityFlagsConnectionRequired)
  65. && (flags & kSCNetworkReachabilityFlagsReachable);
  66. return canReach;
  67. }
Add Comment
Please, Sign In to add comment