iOS network detection reachability uses Demo to detect 2, 3, 4G

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.