2015-06-25 48 views
1

我希望能够检查用户是否连接到WiFi,但未连接到网络。所以基本上我想检查设备设置页面上WiFi按钮的状态,以检查按钮是启用还是禁用。如何检查是否启用了WIFI按钮(iOS)

目前,我可以检查如果WiFi连接到网络或者没有连接到网络的执行以下操作:

BOOL hasWiFiNetwork = NO; 
NSArray *interfaces = CFBridgingRelease(CNCopySupportedInterfaces()); 
for (NSString *interface in interfaces) 
{ 
    NSDictionary *networkInfo = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interface))); 
    if (networkInfo != NULL) 
    { 
     hasWiFiNetwork = YES; 
     break; 
    } 
    else 
    { 
     hasWiFiNetwork = NO; 
     break; 
    } 
} 
+0

https://github.com/tonymillion/Reachability –

+0

@丹可达少了点同上我的代码,其实我是想检查从下面的答案设置 –

+0

页面以及WIFI的按钮的状态,我想就此与你不同意。 –

回答

1

更新

事实证明,这是不可能的,没有API /框架/ BOOL值可以做到这一点,因为苹果没有带附加任何形式的检查,以能力查看开发者是否打开或关闭WiFi。由于很好地说明如下:https://stackoverflow.com/a/12906461/4657588


那么这个SO职位应该是你想要什么:https://stackoverflow.com/a/7938778/4657588

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

NetworkStatus status = [reachability currentReachabilityStatus]; 

if (status == ReachableViaWiFi) { 
    // WiFi 
} 

else { 
    // Either WiFi is off or we are not connected to a WiFi network. 
} 
+0

我知道这会给你的WiFi的状态,但是如果你连接到WiFi,但你没有连接到实际的网络。像你的家庭网络那么它不会给你任何回报。因为它只会检查您是否连接到网络。 –

+0

@ChrisBeckett好吧,如果它没有提供什么,那么你不能假设用户要么没有打开WiFi或没有连接到网络。对于你想要执行的任何警报或测试,这应该足够好。 –

+0

嗯,我测试过了,WiFi已打开,但没有连接到网络,我无法显示消息,因为我可以检查iOS上的实际WiFi设置的状态 - 我尝试执行此操作的目的的GPS位置 –

1

试试这个代码,并使用可达类。

BOOL isConnectedProperly = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWiFi); 
+0

我喜欢这个答案,可达性对于这种类型的任务很棒。 –

+0

这个答案只给出1或0(真或假)的值,这是如果人连接到WiFi网络。 –

+0

原来它不可能:http://stackoverflow.com/a/12906461/4657588 –

1

首先,你需要从apple developer site 下载可达性文件,之后添加这些代码段每有时候俞想要检查的时间。

-(BOOL)isConnectedTointernet{ 
BOOL status = NO; 
Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
int networkStatus = reachability.currentReachabilityStatus; 
status = (networkStatus != NotReachable)? YES:NO; 
return status;} 
+1

我没有试图找出用户是否连接到网络,或没有连接。我只是试图找到iPad上实际WiFi的状态。所以,如果启用了WiFi按钮,则会发送消息,如果它被禁用,则会发送消息。 –

+0

原来它不可能:http://stackoverflow.com/a/12906461/4657588 –

相关问题