1- thêm SystemConfiguration.framework
để dự án của bạn.
2- Tải file từ sau GitHub
Reachability.h
Reachability.m
3- Thêm các tập tin trong dự án của bạn
4 thêm @class Reachability;
trong YourViewController.h
#import <UIKit/UIKit.h>
@class Reachability;
5- thêm biến Reachability* internetReachable;
trong YourViewController.h
#import <UIKit/UIKit.h>
@class Reachability;
@interface YourViewController : UIViewController {
Reachability* internetReachable;
}
6- thêm Reachability.h
trong YourViewController.m
#import "YourViewController.h"
#import "Reachability.h"
7- add sau dòng trong -(void)ViewDidLoad
trong YourViewController.m
-(void)ViewDidLoad {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
}
8- add chức năng sau đây sau khi -(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;
}
}
}
Bây giờ bao giờ hết y thay đổi kết nối internet, bạn sẽ thấy bảng điều khiển đăng nhập.
Bản sao có thể có của: http://stackoverflow.com/questions/784582/easiest-way-to-determine-whether-iphone-internet-connection-is-available – 0x8badf00d