检测设备是否超出wifi范围

时间:2012-12-19 10:38:31

标签: iphone objective-c ios ipad

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

2 个答案:

答案 0 :(得分:2)

这是您可以用来查找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);
    }
}

**

答案 1 :(得分:0)

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

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