IOS network detection Reachability uses Demo to detect 2, 3, 4G, iosreachability

Source: Internet
Author: User

IOS network detection Reachability uses Demo to detect 2, 3, 4G, iosreachability

You can download this Demo https://github.com/JanzTam/Reachability_Demo At Github

 

First, introduce the Reachability class of the system. If you do not know how to introduce it, In Xcode, press shift + command + 0 and search for Reachability. You can see the project selected in the figure and download it.


Reachability.png and import related. h files # import <CoreTelephony/CTTelephonyNetworkInfo. h>

After the introduction, modify the NetworkStatus enumeration in the Reachability. h file,

typedef enum : NSInteger {    NotReachable = 0,    ReachableViaWiFi,    ReachableViaWWAN,    kReachableVia2G,    kReachableVia3G,    kReachableVia4G} NetworkStatus;

Modify the Reachability. m file.
Find

- (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;                }                else if ([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 to use it?

Add in AppDelegate

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// set the status bar [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault]; // monitor network conditions [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (reachabilityChanged :) name: jsonobject: nil]; hostReach = [Reachability failed: @ "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;    }}

Related Article

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.