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

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

【问题讨论】:

标签: iphone objective-c ios ipad


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2015-05-31
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多