简体   繁体   中英

How to check the internet Connection in iPhone without Reachability classes?

I want to check the internet connection in my iPhone without the use of Reachability Classes. i want to constantly check the connection when a particular event is triggered in my view. I also want to determine if the connection is from a Wifi or through 2G or 3G connection. I have already tried using Reachability classes. But these classes just return the value if the Wifi is On (Though the net cable is unplugged from the Wifi Router). I have tried

[Reachability reachabilityForInternetConnection]

and

[Reachability reachabilityWithHostName:@"www.google.com"];

But the above methods doesn't seem to work properly inspite of network disconnections.

Also is there any way we can determine the type of netwrok 2G or 3G in iOS6? I know that we have Core Telephony Framework that works only in iOS 7. But i just want to know if i can determine the cellular network iOS 6.0. Please help me.

Working code without reachability classes and also working in iOS 6:

- (NSNumber *) dataNetworkTypeFromStatusBar {

    UIApplication *app = [UIApplication sharedApplication];
    NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"]    subviews];
    NSNumber *dataNetworkItemView = nil;

    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }
    return [dataNetworkItemView valueForKey:@"dataNetworkType"];
}

And the value keys I've found so far:

0 = No wifi or cellular

1 = 2G and earlier?

2 = 3G?

3 = 4G

4 = LTE

5 = Wifi

Or in iOS7 use CoreTelephony framework

CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
[NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification 
                                                object:nil 
                                                 queue:nil 
                                            usingBlock:^(NSNotification *note) 
{
    NSLog(@"New Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM