Reachability airplane mode (3G) vs. Wifi

2019-01-20 20:04发布

问题:

I know the Reachability code will provide if the phone has access to Wifi or 3G network.

However, if the phone has 3G on and Wifi, the Reachability code defaults to saying the phone has Wifi on and I can't detect if 3G network is enabled or not (airplane mode on or off).

I need to find out specifically if 3G mode is on or off when Wifi is also on.

Here is the code:

Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus status = [reachability currentReachabilityStatus];

NSLog(@"status: %u", status);

if(status == NotReachable)  //status = 0
{
    //No internet
    NSLog(@"nothing is reachable");
}
if (status == ReachableViaWiFi) //status = 1
{
    //WiFi
    NSLog(@"Wifi is available");
}
if (status == ReachableViaWWAN) //status = 2
{
    //3G
    NSLog(@"3G is available");
}
NSLog(@"%@", s);

Basically status is returning 1 even if the 3G is on as well as Wifi.

Is this possible?

Help would greatly be appreciated.

回答1:

Update: It seems like it is not possible as of now. However you can try the following codes, just in case if it helps.

Code1: Just a thought. Haven't tried this.

if (status == ReachableViaWiFi) //status = 1
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]) {
        NSLog(@"Wifi and 3G are available");
    } else {
        NSLog(@"Wifi is available, but 3G is not available");
    }
}

Code 2:

For internal apps you can use this approach as mentioned here,

Note: This uses private API, so your app will be rejected if you use it in appstore app.

Copy paste the below contents into RadioPreferences.h

@protocol RadiosPreferencesDelegate
-(void)airplaneModeChanged;
@end


@interface RadiosPreferences : NSObject
{
    struct __SCPreferences *_prefs;
    int _applySkipCount;
    id <RadiosPreferencesDelegate> _delegate;
    BOOL _isCachedAirplaneModeValid;
    BOOL _cachedAirplaneMode;
    BOOL notifyForExternalChangeOnly;
}

- (id)init;
- (void)dealloc;
@property(nonatomic) BOOL airplaneMode;
- (void)refresh;
- (void)initializeSCPrefs:(id)arg1;
- (void)notifyTarget:(unsigned int)arg1;
- (void)synchronize;
- (void *)getValueForKey:(id)arg1;
- (void)setValue:(void *)arg1 forKey:(id)arg2;
@property(nonatomic) BOOL notifyForExternalChangeOnly; // @synthesize notifyForExternalChangeOnly;
@property(nonatomic) id <RadiosPreferencesDelegate> delegate; // @synthesize delegate=_delegate;

@end 

Then try as below.

id rp = [[RadiosPreferences alloc] init];
BOOL status = [rp airplaneMode];
return status;


回答2:

Currently there is no way to do this on the iPhone following Apple guidelines and without using their private API.