检测设备是不是超出 wifi 范围

Posted

技术标签:

【中文标题】检测设备是不是超出 wifi 范围【英文标题】:Detect if a device goes out of a wifi range检测设备是否超出 wifi 范围 【发布时间】:2012-12-19 10:38:30 【问题描述】:

是否有任何事件或任何其他方式可以检测 iPad 或 iPhone 何时超出特定 WiFi 范围。我搜索了这个,只能找到 Reachability 类,而且我还必须在后台不断检查连接性,这在我的情况下不是首选。任何帮助将不胜感激...

【问题讨论】:

如果你应该做的方式是“不首选”,还有什么是首选? 检查***.com/questions/11083369/ios-wifi-notification-api 您应该重新阅读可达性示例和文档。当连接发生变化时,您的代码会收到通知 - 您不必不断轮询它 查看这个答案***.com/questions/339089/… @ParasJoshi 非常感谢您提供如此快速的建议,请尝试一下。 【参考方案1】:

这是您可以用来查找 wifi/互联网连接的示例代码

.h 文件

@class Reachability;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

    Reachability* wifiReach;

.m 文件

**

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
        self.viewController = [[HPViewController alloc] initWithNibName:@"HPViewController_iPhone" bundle:nil];
     else 
        self.viewController = [[HPViewController alloc] initWithNibName:@"HPViewController_iPad" bundle:nil];
    

    _navCon =[[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = _navCon;

    // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
    // method "reachabilityChanged" will be called.
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    //Change the host name here to change the server your monitoring
    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
    [wifiReach startNotifier];
    [self showAlertOfInternetConncetion: wifiReach];

    return YES;

//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note

    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self showAlertOfInternetConncetion: curReach];

- (void)showAlertOfInternetConncetion : (Reachability*) curReach

    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    switch (netStatus)
    
        case NotReachable:
        
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet connection" message:@"Your internet connection has stopped working. Please check it." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Exit App", nil];
            [alert show];
            [alert release];
        
        case ReachableViaWWAN:
        
            NSLog(@"ReachableVia WWAN");
        
        case ReachableViaWiFi:
        
            NSLog(@"ReachableVia WiFi");
        
       

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

    if (buttonIndex == 0) 
        exit(0);
    

**

【讨论】:

@spider1983 你试过这个并发现它有效吗?如果没有,请不要接受答案,直到您确定他们回答了您的问题。另外,这只是你说你已经找到的可达性代码,它对你不起作用..... @NickBull 哦,好吧,我知道自己的方式已经退出了……我会纠正的……对此我很抱歉。 @NickBull 我将此代码放在 Appdelegate.m 中,每当我超出 WiFi 范围时,我发现它可以通过显示我在代码中编写的 UIAlertView 来工作 @hpiOSCoder 我不怀疑这是一个正确的答案——我敢肯定!我的意思是,OP 在检查自己是否正确之前已将其标记为正确。我也只是在向他指出,这是他已经驳回的不合适的事情!你的回答没有错! @NickBull 从你那里了解到,不检查不标记为已接受,也不是我不想使用可达性课程,只是我不想参与我的线程,而且我不知道可达性类中的通知器...无论如何感谢所有建议。【参考方案2】:

使用 Apple 的 SCNetworkReachability 类。阅读SCNetworkReachability documentation。 测试使用:

(ReachabilityInst.currentReachabilityStatus().value == ReachableViaWiFi.value)

【讨论】:

以上是关于检测设备是不是超出 wifi 范围的主要内容,如果未能解决你的问题,请参考以下文章

CoreBluetooth:检测设备超出范围/连接超时

使用 .edgesIgnoringSafeArea() 时,SwiftUI 布局超出设备范围

是否可以获得wifi热点范围内的客户端设备的mac地址?

当我在真实设备中频繁推送/返回视图控制器时出现错误“索引超出范围” - Swift

检测 CGAffineTransformed 视图是不是超出屏幕/UIView 的范围

如何检测 WiFi 网络中是不是存在设备?