2012-12-19 42 views
0

有任何事件或任何其他方式來檢測iPad或iPhone何時超出特定WiFi範圍。我搜索了這一點,只能找到可達性類,這也是我將不得不在連續檢查連接,這是不是在我的情況下首選。任何幫助將不勝感激...檢測設備是否超出WiFi範圍

+0

好,如果你應該這樣做的方式是「不首選」,還有什麼是首選? – jimpic

+0

連續檢查wifi會減慢我的應用程序,因爲我已經有太多的後臺事件正在進行。如果我能得到一個事件,那麼將是最好的事情。 – spider1983

+1

檢查http://stackoverflow.com/questions/11083369/ios-wifi-notification-api – jimpic

回答

2

這是示例代碼,你可以用它來找到WIFI /互聯網conncetivity

.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); 
    } 
} 

**

+0

感謝您的答案將今天自己嘗試.... – spider1983

+0

@ spider1983你試過這個,發現它的工作原理?如果沒有,請不要接受答案,直到您確定他們回答您的問題。此外,這只是你說你已經找到的可達性代碼,它不會爲你工作..... –

+0

@NickBull哦確定我確定我已經退出知道方式本身...我會糾正.. .. 。 我爲此感到抱歉。 – spider1983

0

使用Apple的SCNetworkReachability類。閱讀SCNetworkReachability documentation。 測試使用:

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