2014-12-05 136 views
0

我有一个代码,检查互联网连接是否存在。如果没有互联网,我会显示警报。下面是代码:iOS - isViewLoaded崩溃

- (void)testInternetConnection 
{ 
    __unsafe_unretained typeof(self) weakSelf = self; 
    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.your-voc.com"]; 

    // Internet is reachable 
    internetReachableFoo.reachableBlock = ^(Reachability*reach) 
    { 
     // Update the UI on the main thread 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      internetActivated = YES; 
      NSLog(@"Yayyy, we have the interwebs!"); 
     }); 
    }; 

    // Internet is not reachable 
    internetReachableFoo.unreachableBlock = ^(Reachability*reach) 
    { 
     // Update the UI on the main thread 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      internetActivated = NO; 
      if(alertLoaded == NO){ 
       if(weakSelf.isViewLoaded && weakSelf.view.window){ 
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Pas de connexion internet" message:@"Une connexion est requise pour utiliser l'application" delegate:weakSelf cancelButtonTitle:nil otherButtonTitles:@"Réessayer", @"Mode hors-ligne", nil]; 
        [alert show]; 
        alertLoaded = YES; 
       } 
      } 
      NSLog(@"Someone broke the internet :("); 
     }); 
    }; 

    [internetReachableFoo startNotifier]; 
} 

我使用isViewLoaded和view.window就一定要仅在当前窗口是加载和显示一个显示警报。

但有些时候,当我关闭wifi,我的模拟器崩溃,出现以下错误;

-[UIScrollViewDelayedTouchesBeganGestureRecognizer isViewLoaded]: unrecognized selector sent to instance 0x7fbe73da4060 
2014-12-05 14:16:57.142 [838:117938] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIScrollViewDelayedTouchesBeganGestureRecognizer isViewLoaded]: unrecognized selector sent to instance 0x7fbe73da4060' 

什么是错?

非常感谢

更新:我使用的托尼百万版的可达性,所以testInternetConnection方法被称为每当网络状态的变化。

+0

您能否添加更多代码?就像你如何调用这个方法一样? – 2014-12-05 13:32:50

+0

你走了,更新了代码。 – 2014-12-05 13:38:04

+0

你试过__strong typeof(self)weakSelf = self; – 2014-12-05 13:47:59

回答

0

您的weakSelf变量可能是一个弱指针,并且在代码达到此点时已经释放。

+0

看着更新的代码,你觉得怎么样?感谢您的帮助 – 2014-12-05 13:35:25

+0

是的,具有'testInternetConnection'方法的对象被释放。 Reachability类没有问题。我们需要看看你在哪里调用'testInternetConnection'并用object来结束。 – ugur 2014-12-05 13:47:37

+0

我在viewDidLoad和viewDidAppear中调用我的testInternetConnection! – 2014-12-05 17:46:36