Ios determines the network
I believe everyone knows the importance of the network. This article introduces how ios judges whether the network is good or bad. It is very easy to share with you because it is used in development. The Reachability encapsulation class is used for implementation. You can download the Reachability. m and Reachability. H files online. Please leave a message if you need them.
Code:
Implementation in AppDelegate:
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[uiappswalloc] initWithFrame: [UIScreenmainScreen] bounds];
Self. window. backgroundColor = [UIColorwhiteColor];
// Initialize the login page
LoginViewController * loginCtrl = [[LoginViewControlleralloc] init];
Self. window. rootViewController = loginCtrl;
// Determine the network
[[Nsnotificationcenterdefacenter] addObserver: self
Selector: @ selector (reachabilityChanged :)
Name: kReachabilityChangedNotification
Object: nil];
Reachability * reach = [ReachabilityreachabilityWithHostname: @ "www.baidu.com"];
[ReachstartNotifier];
// MainViewController * mainController = [[MainViewController alloc] init];
// Self. window. rootViewController = mainController;
[Self. windowmakeKeyAndVisible];
Return YES;
}
// Notification
-(Void) reachabilityChanged :( NSNotification *) note
{
Reachability * reach = [note object];
If ([reach isReachable])
{
NSLog (@ "Notification Says Reachable ");
}
Else
{
UIAlertView * alertView = [[UIAlertViewalloc] initWithTitle: nilmessage: @ "Network disconnected" delegate: nilcancelButtonTitle: @ "OK" otherButtonTitles: nil];
[AlertViewshow];
NSLog (@ "Notification Says Unreachable ");
}
}
The code is very simple. You can try it !!!