2011-10-18 40 views
5

崩潰時,我使用這個代碼,以檢查是否有互聯網連接,但我得到一個崩潰說+[Reachability reachabilityForInternetConnection]: unrecognized selector sent to class 0xcbe0c8應用程序中使用的類的可達性檢查互聯網連接

我已經導入Reachability .h/.m和systemconfig框架。崩潰是線self.internetRechable = [[Reachability reachabilityForInternetConnection] retain];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; 

    self.internetRechable = [[Reachability reachabilityForInternetConnection] retain]; 
    [self.internetRechable startNotifier]; 

    // check if a pathway to a random host exists 
    self.hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain]; 
    [self.hostReachable startNotifier]; 

- (void) checkNetworkStatus:(NSNotification *)notice 
{ 
    // called after network status changes 

    NetworkStatus internetStatus = [self.internetRechable currentReachabilityStatus]; 
    switch (internetStatus) 
    { 
     case NotReachable: 
     { 
      NSLog(@"The internet is down."); 
//   self.internetActive = NO; 
      break; 
     } 
     case ReachableViaWiFi: 
     { 
      NSLog(@"The internet is working via WIFI."); 
//   self.internetActive = YES; 
      break; 
     } 
     case ReachableViaWWAN: 
     { 
      NSLog(@"The internet is working via WWAN."); 
//   self.internetActive = YES; 
      break; 
     } 
    } 

    NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus]; 
    switch (hostStatus) 
    { 
     case NotReachable: 
     { 
      NSLog(@"A gateway to the host server is down."); 
//   self.hostActive = NO; 
      break; 
     } 
     case ReachableViaWiFi: 
     { 
      NSLog(@"A gateway to the host server is working via WIFI."); 
//   self.hostActive = YES; 
      break; 
     } 
     case ReachableViaWWAN: 
     { 
      NSLog(@"A gateway to the host server is working via WWAN."); 
//   self.hostActive = YES; 
      break; 
     } 
    } 
} 
+0

不,我已經完成了'@ class Reachability'並且我也導入了 – Jon

+0

是的,我導入了文件,並且完成了'#import「Reachability.h」等。 – Jon

+0

我做到了,崩潰在'Reachability * newReach = [可達性reachabilityForInternetConnection];' – Jon

回答

1

確保您Reachability的版本爲2.2,幾件事情最近改變,如果你不使用2.2可能導致thiscrash。

這裏有鏈接的 Reachability.hReachability.m

而且version2.2,如果有幫助,對於這個相同的任務,我的繼承人的工作代碼:。

在我appDidFinishLaunchinghostReachableinternetReachable是我的應用程序委託的實例變量):

//.... 
if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == NotReachable) { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; 
    internetReachable = [[Reachability reachabilityForInternetConnection] retain]; 
    [internetReachable startNotifier]; 
    hostReachable = [[Reachability reachabilityWithHostName:@"google.com"] retain]; 
    [hostReachable startNotifier]; 
} 

然後,回調:

- (void)checkNetworkStatus:(NSNotification *)notice { 
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
    switch (internetStatus) { 
     case NotReachable: 
      self.internetActive = NO; 
      break; 
     case ReachableViaWiFi: 
      self.internetActive = YES; 
      break; 
     case ReachableViaWWAN: 
      self.internetActive = YES; 
      break; 
    } 
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 
    switch (hostStatus) { 
     case NotReachable: 
      self.hostActive = NO; 
      break; 
     case ReachableViaWiFi: 
      self.hostActive = YES; 
      break; 
     case ReachableViaWWAN: 
      self.hostActive = YES; 
      break; 
    } 
    if (internetActive && hostActive) { 
     [self refreshAllData]; 
    } 
} 
-1

您應該關閉ARC關閉。 轉到建設階段,選擇此呼籲,並在右邊雙擊並鍵入

-fno -objc -arc 

我認爲你可以下降得保留代碼。