我知道如果手机可以访问 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。
这可能吗?
更新:目前看来还不可能。不过,您可以尝试以下代码,以防万一它有帮助。
代码1:只是一个想法。没试过这个。
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;
目前,如果不使用其私有 API,则无法在 iPhone 上按照 Apple 指南执行此操作。