You can download this Demo Https://github.com/JanzTam/Reachability_Demo on GitHub
First, the introduction of the System Reachability class, do not know how to introduce the words, in Xcode, press shift+command+0, search reachability, see the diagram of the selected project download can be.
Reachability.png and import the relevant. h file #import<coretelephony/cttelephonynetworkinfo.h>
After introduction, the enumeration of NetworkStatus is modified inside the Reachability.h file,
typedef enum : NSInteger { NotReachable = 0, ReachableViaWiFi, ReachableViaWWAN, kReachableVia2G, kReachableVia3G, kReachableVia4G} NetworkStatus;
Under Modify the REACHABILITY.M file
Found it
- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
Modify the following code
if ((Flags & kscnetworkreachabilityflagsiswwan) = = Kscnetworkreachabilityflagsiswwan) {if ([[[[Uidevice Currentdevice] systemversion]Floatvalue] >=7.0) {Cttelephonynetworkinfo * info = [[Cttelephonynetworkinfo alloc] init]; NSString *currentradioaccesstechnology = info.currentradioaccesstechnology;if (currentradioaccesstechnology) {if ([Currentradioaccesstechnology Isequaltostring:ctradioaccesstechnologylte]) {returnvalue = kreachablevia4g; }Elseif ([currentradioaccesstechnology Isequaltostring:ctradioaccesstechnologyedge] | | [Currentradioaccesstechnology Isequaltostring:ctradioaccesstechnologygprs]) {returnvalue = kreachablevia2g;} else { returnvalue = kreachablevia3g;} return returnvalue;}} if ((Flags & kscnetworkreachabilityflagstransientconnection) = = Kscnetworkreachabilityflagstransientconnection) { if (Flags & kscnetworkreachabilityflagsconnectionrequired) = = kscnetworkreachabilityflagsconnectionrequired) { ReturnValue = kreachablevia2g; return returnvalue;} returnvalue = kreachablevia3g; return returnvalue;} returnvalue = Reachableviawwan;}
How do I use it?
Join in the appdelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ //设置状态栏 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; // 监测网络情况 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"]; [hostReach startNotifier];}
-(void) reachabilitychanged: ( Nsnotification *) note{reachability* Curreach = [Note Object]; nsparameterassert ([Curreach iskindofclass: [reachability class]]); NetworkStatus status = [Curreach currentreachabilitystatus]; switch (status) {case notreachable: break; case Reachableviawifi: case Reachableviawwan: break; case kreachablevia2g: break; case kreachablevia3g: break; case kreachablevia4g: break;}
iOS network detection reachability uses Demo to detect 2, 3, 4G