Last Updated: February 25, 2016
·
9.989K
· e0d

Check internet connection without Reachability (Objective C, iOS)

Проверка Интернет соединения без Reachability (Objective C, iOS).
Метод отправляет запрос на google.com и получает ответ:

 - (BOOL)connectedToInternet
{
 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];

 return ([response statusCode] == 200) ? YES : NO;
} 

Используйте этот код, там где необходимо проверить соединение.

if([self connectedToInternet] == NO) 
 { 
 // Not connected to the internet
 }
 else
 {
 // Connected to the internet
 }

2 Responses
Add your response

Воо! То что нужно, спасибо

over 1 year ago ·

Огромное спасибо.

over 1 year ago ·