The network state of the device for reachability 1.5.typedef enum { notreachable = 0, //no connection Reachableviacar Rierdatanetwork,//Use 3G/GPRS network reachableviawifinetwork //Use WiFi network} networkstatus;//the Device for reachability 2.0.typedef enum { notreachable = 0, //No connection Reachableviawifi, //using 3G/GPRS Network Reachableviawwan //Use WiFi network} networkstatus;
For example, to detect the continuation status of a particular site, you can use the following code:
reachability *r = [reachabilityforlocalwifi];
switch ([R Currentreachabilitystatus]) {case notreachable: //No network connection break ; Case Reachableviawwan: //Use 3G network break ; Case Reachableviawifi: //Use WiFi network break ;}
Add Reachability.h and REACHABILITY.M to your project and use Systemconfiguration.framework to make it work.
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{
if ([reachability reachabilityforinternetconnection].currentreachabilitystatus==notreachable) | | ([reachability reachabilityforlocalwifi].currentreachabilitystatus==notreachable)] {
Uialertview *alert=[[uialertview alloc]initwithtitle:@"Warning" message:@ "your device is temporarily free of network! "Delegate:self cancelbuttontitle:@"determines "Otherbuttontitles:nil";
[Alert show];
}
}
Add real-time monitoring of network changes
Referencing in APPDELETEGATE.M
#import "Reachability.h"
@interface Appdelegate ()
@property (Nonatomic,strong) reachability *conn;
@end
@implementation Appdelegate
-(void) reachabilitychanged: (nsnotification *) Note
{
reachability* Curreach = [Note Object];
Nsparameterassert ([Curreach iskindofclass: [reachability class]]);
NetworkStatus status = [Curreach currentreachabilitystatus];
Switch (status)
{
Case Notreachable:
No network connection
NSLog (@ "Network is not Connected");
Break
Case Reachableviawwan:
Using the 2G/3G Network
NSLog (@ "Connected cellular data");
Break
Case Reachableviawifi:
Use WiFi network
NSLog (@ "Connected WiFi");
Break
}
}
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
Add monitoring
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) Name: Kreachabilitychangednotification Object:nil];
Self.conn = [reachability reachabilityforinternetconnection];
[Self.conn Startnotifier];
return YES;
}
IOS---Detect network status