1. Add the framework:
Add systemconfiguration. Framework to the project.
2. Download http://download.csdn.net/detail/svrsimon/4458137
Copy reachability. h and reachability. m to the project.
3. Called code:
-(Nsstring *) getcurrntnet
{
Nsstring * result;
Reachability * r = [reachability reachabilitywithhostname: @ "www.apple.com"];
Switch ([R currentreachabilitystatus]) {
Casenotreachable: // No network connection
Result = nil;
Break;
Casereachableviawwan: // use a 3G network
Result = @ "3G ";
Break;
Casereachableviawifi: // use a Wi-Fi network
Result = @ "WiFi ";
Break;
}
Return result;
}
The above code is tested on the simulator of the Virtual Machine. If there is an exception, cannot the simulator be used to test network connectivity?
In addition, no matter whether a network cable is inserted or not, the WiFi of the simulator is always connected, regardless of whether the network of the computer is disconnected (also in a virtual machine)
+ (Bool) isenablewifi {// always return YES
Return ([[reachability reachabilityforlocalwifi] currentreachabilitystatus]! = Notreachable );
}
3G/GPRS is always disconnected.
+ (Bool) isenable3g {// No is always returned
Return ([[reachability reachabilityforinternetconnection] currentreachabilitystatus]! = Notreachable );
}