Reputation: 1476
I have a code that checks if an internet connection is there. If there is no internet, I display an alert. Here is the code:
- (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];
}
I use the isViewLoaded and view.window to be sure to only display the alert if the current window is the one that is loaded and displayed.
But some times, when I shut down the wifi, my simulator crashes with the following error;
-[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'
What is wrong?
Thanks a lot
UPDATE: I use Tony Million's version of Reachability, so the testInternetConnection method is called whenever the internet status changes.
Upvotes: 0
Views: 472
Reputation: 842
Your weakSelf
variable is probably a weak pointer and is already released when code reaches this point.
Upvotes: 0