我有一个代码,检查互联网连接是否存在。如果没有互联网,我会显示警报。下面是代码: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方法被称为每当网络状态的变化。
您能否添加更多代码?就像你如何调用这个方法一样? – 2014-12-05 13:32:50
你走了,更新了代码。 – 2014-12-05 13:38:04
你试过__strong typeof(self)weakSelf = self; – 2014-12-05 13:47:59