OGeek|极客世界-中国程序员成长平台

标题: ios - 可达性通知多个警报 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:56
标题: ios - 可达性通知多个警报

我正在使用 Tony Million 的 Reachability 版本(问题与 Apple 版本的 Reachability 相同)来检查我的应用程序是否有事件的互联网连接。

这是我想要的:
当 View 加载或出现时,它会检查是否有互联网连接。如果没有,如果显示警报,单击时会再次尝试,直到有事件连接。

如果有连接,则 View 正常加载。当 Reachability 通知连接丢失时,它会再次显示相同的警报。 这是我的实际代码:

//in the implementation:
BOOL internetActivated;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self testInternetConnection];
    NSLog(@"%d", internetActivated);
    if(internetActivated == NO)
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle"as 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)viewDidAppearBOOL)animated
{
    [self testInternetConnection];
    if(internetActivated == NO)
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle"as 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)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)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"as 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) 即使我有互联网连接,testInternetConnection 方法中的 UIAlertView 也会不断被调用。实际上:第二个警报被调用了三到四次(在 testInternet 方法中),然后第一个警报 View 被调用了三到四次,即 viewDidLoad 方法中的那个。

顺便说一句,我注意到这个 NSLog: NSLog(@"Yayyy, we have the interwebs!"); 在每次 [self testInternetConnection] 调用中被调用多次。

我完全搞乱了警报调用,这让我发疯了!

感谢您的帮助

更新:

我设法通过在加载时将 BOOL 设置为 true 并在单击离开时将其设置为 false 来避免多个警报,以避免多个警报堆积。唯一的事情是我希望 internetActivated BOOL 在我检查它之前更新 if...



Best Answer-推荐答案


从 viewdidload 中删除代码

Bool alertIsShowing = NO;

- (void)viewDidAppearBOOL)animated
{
    if (! alertIsShowing)
        [self testInternetConnection];
}

- (void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)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:@"as 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];
}

关于ios - 可达性通知多个警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27137296/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4