我正在使用Tony Million的Reachability版本(问题与Apple的Reachability版本相同)来检查我的应用程序是否存在活动的互联网连接。
这就是我想要的:
当视图加载或出现时,它会检查是否存在互联网连接。如果没有,如果显示警报,并且单击,则再次尝试,直到存在活动连接。
如果存在连接,则视图正常加载。当Reachability通知连接丢失时,它会再次显示相同的警报。 这是我的实际代码:
//in the implementation:
BOOL internetActivated;
- (void)viewDidLoad
{
[super viewDidLoad];
[self testInternetConnection];
NSLog(@"%d", internetActivated);
if(internetActivated == NO)
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Pas de connexion internet" message:@"Une connexion est requise pour utiliser l'application" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Réessayer", nil];
[alert show];
}
else {
[self onAppearFunction];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[self testInternetConnection];
if(internetActivated == NO)
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Pas de connexion internet" message:@"Une connexion est requise pour utiliser l'application" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Réessayer", nil];
[alert show];
}
else {
[self onAppearFunction];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
{
[self viewDidLoad];
}
}
- (void)testInternetConnection
{
__unsafe_unretained typeof(self) weakSelf = self;
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.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;
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", nil];
[alert show];
NSLog(@"Someone broke the internet :(");
});
};
[internetReachableFoo startNotifier];
}
多个问题:
1)InternetActivated以“NO”开始,即使我在测试if条件之前调用[self testInternetConnection]
。它应该更新为 YES ,不是吗?
2)即使我有互联网连接,UIAlertView
方法中的testInternetConnection
仍会被调用。实际上:调用第二个警报的三到四次(在testInternet方法中),然后调用第一个警报视图的三到四次,即viewDidLoad
方法中的一个。
顺便说一下,我注意到这个NSLog:NSLog(@"Yayyy, we have the interwebs!");
每次[self testInternetConnection]调用都会调用多次。
我完全弄乱了警报电话,这让我发疯了!
感谢您的帮助
更新:
我设法通过在加载BOOL时将BOOL设置为true来避免多重警报,并在单击时将其设置为false以避免堆叠多个警报。唯一的问题是我想在{... 1}} BOOL更新之前更新{<1}} BOOL。
答案 0 :(得分:2)
从viewdidload中删除代码
Bool alertIsShowing = NO;
- (void)viewDidAppear:(BOOL)animated
{
if (! alertIsShowing)
[self testInternetConnection];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
alertIsShowing = NO;
if (buttonIndex == 0)
{
[self testInternetConnection];
}
}
- (void)testInternetConnection
{
__unsafe_unretained typeof(self) weakSelf = self;
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
[self onAppearFunction];
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
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", nil];
[alert show];
alertIsShowing = YES;
NSLog(@"Someone broke the internet :(");
});
};
[internetReachableFoo startNotifier];
}