可及性飞机模式(3G)与WiFi



我知道如果手机可以访问WiFi或3G网络,则可以提供可及性。

但是,如果手机打开3G和wifi,则可以默认情况下的可访问性代码说手机已打开WiFi,我无法检测到3G网络是否已启用(飞机模式打开或关闭)。

我需要具体找出3G模式是否在WiFi也打开时打开或关闭。

这是代码:

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);

基本上状态正在返回1,即使3G也打开以及wifi。

这是可能的吗?

帮助您将不胜感激。

更新:到目前为止似乎不可能。但是,您可以尝试以下代码,以防万一。

code1:只是一个想法。还没有尝试过。

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");
    }
}

代码2:

对于内部应用程序,您可以使用此方法,如下所述,

注意:使用私有API,因此如果您在AppStore应用中使用它,您的应用将被拒绝。

将以下内容复制到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 

然后尝试以下。

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

当前无法按照Apple准则在iPhone上执行此操作,而无需使用其私有API。

最新更新