0

我知道如果手机可以访问 Wifi 或 3G 网络,将提供可达性代码。

但是,如果手机有 3G 和 Wifi,则可达性代码默认显示手机有 Wifi,我无法检测到 3G 网络是否启用(飞行模式打开或关闭)。

我需要特别了解当 Wifi 也打开时 3G 模式是打开还是关闭。

这是代码:

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

即使 3G 和 Wifi 都打开,基本上状态也会返回 1。

这可能吗?

帮助将不胜感激。

4

2 回答 2

1

更新:目前看来这是不可能的。但是,您可以尝试以下代码,以防万一。

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,因此如果您在应用商店应用程序中使用它,您的应用程序将被拒绝。

复制粘贴以下内容到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;
于 2012-10-26T22:37:46.903 回答
1

目前没有办法在 iPhone 上按照 Apple 指导方针并且不使用其私有 API 来执行此操作。

于 2012-11-30T23:02:58.460 回答