1 - добавьте SystemConfiguration.framework
к вашему проекту.
2 - Скачать следующие файлы из GitHub
Reachability.h
Reachability.m
3- Добавьте эти файлы в свои проекты
4 - добавить @class Reachability;
в YourViewController.h
#import <UIKit/UIKit.h>
@class Reachability;
5 - добавить переменную Reachability* internetReachable;
в YourViewController.h
#import <UIKit/UIKit.h>
@class Reachability;
@interface YourViewController : UIViewController {
Reachability* internetReachable;
}
6 - добавить Reachability.h
в YourViewController.m
#import "YourViewController.h"
#import "Reachability.h"
7 - добавить следующие строки в -(void)ViewDidLoad
в YourViewController.m
-(void)ViewDidLoad {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
}
8 - добавить следующую функцию после -(void)viewDidLoad
-(void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
}
Теперь при каждой смене интернет-соединения вы будете видеть вход в консоль.