我有一个代码,用于检查是否存在互联网连接。如果没有互联网,我会显示警报。这是代码:
- (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'
有什么问题?
非常感谢
更新:我使用Tony Million的Reachability版本,因此只要互联网状态发生变化,就会调用testInternetConnection方法。
答案 0 :(得分:0)
您的weakSelf
变量可能是一个弱指针,并且在代码到达此点时已经释放。