菜鸟教程小白 发表于 2022-12-12 12:56:29

ios - 可达性通知多个警报


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

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

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

<pre><code>//in the implementation:
BOOL internetActivated;

- (void)viewDidLoad
{
    ;
    ;
    NSLog(@&#34;%d&#34;, internetActivated);
    if(internetActivated == NO)
    {
      UIAlertView * alert = [ initWithTitle:@&#34;Pas de connexion internet&#34; message:@&#34;Une connexion est requise pour utiliser l&#39;application&#34; delegate:self cancelButtonTitle:nil otherButtonTitles:@&#34;Réessayer&#34;, nil];
      ;
    }
    else {
      ;
    }
}


- (void)viewDidAppear:(BOOL)animated
{
    ;
    if(internetActivated == NO)
    {
      UIAlertView * alert = [ initWithTitle:@&#34;Pas de connexion internet&#34; message:@&#34;Une connexion est requise pour utiliser l&#39;application&#34; delegate:self cancelButtonTitle:nil otherButtonTitles:@&#34;Réessayer&#34;, nil];
      ;
    }
    else {
      ;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0)
    {
      ;
    }
}

- (void)testInternetConnection
{
    __unsafe_unretained typeof(self) weakSelf = self;
    internetReachableFoo = ;

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
      // Update the UI on the main thread
      dispatch_async(dispatch_get_main_queue(), ^{
            internetActivated = YES;
            NSLog(@&#34;Yayyy, we have the interwebs!&#34;);
      });
    };

    // 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 = [ initWithTitle:@&#34;Pas de connexion internet&#34; message:@&#34;Une connexion est requise pour utiliser l&#39;application&#34; delegate:weakSelf cancelButtonTitle:nil otherButtonTitles:@&#34;Réessayer&#34;, nil];
            ;
            NSLog(@&#34;Someone broke the internet :(&#34;);
      });
    };

    ;
}
</code></pre>

<p>多个问题:<br/>
1) InternetActivated 以“NO”开头,即使我在测试 if 条件之前调用了 <code></code>。它应该更新为<em>YES</em>,不是吗? </p>

<p>2) 即使我有互联网连接,<code>testInternetConnection</code> 方法中的 <code>UIAlertView</code> 也会不断被调用。实际上:第二个警报被调用了三到四次(在 testInternet 方法中),然后第一个警报 View 被调用了三到四次,即 <code>viewDidLoad</code> 方法中的那个。</p>

<p>顺便说一句,我注意到这个 NSLog: <code>NSLog(@"Yayyy, we have the interwebs!");</code>
在每次 调用中被调用多次。 </p>

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

<p>感谢您的帮助</p>

<p>更新:</p>

<p>我设法通过在加载时将 BOOL 设置为 true 并在单击离开时将其设置为 false 来避免多个警报,以避免多个警报堆积。唯一的事情是我希望 <code>internetActivated</code> BOOL 在我检查它之前更新 if...</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>从 viewdidload 中删除代码</p>

<pre><code>Bool alertIsShowing = NO;

- (void)viewDidAppear:(BOOL)animated
{
    if (! alertIsShowing)
      ;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    alertIsShowing = NO;
    if (buttonIndex == 0)
    {
      ;
    }
}

- (void)testInternetConnection
{
    __unsafe_unretained typeof(self) weakSelf = self;
    internetReachableFoo = ;

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
      // Update the UI on the main thread
      dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@&#34;Yayyy, we have the interwebs!&#34;);
            ;
      });
    };

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach)
    {
      // Update the UI on the main thread
      dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView * alert = [ initWithTitle:@&#34;Pas de connexion internet&#34; message:@&#34;Une connexion est requise pour utiliser l&#39;application&#34; delegate:weakSelf cancelButtonTitle:nil otherButtonTitles:@&#34;Réessayer&#34;, nil];
            ;
            alertIsShowing = YES;
            NSLog(@&#34;Someone broke the internet :(&#34;);
      });
    };

    ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 可达性通知多个警报,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27137296/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27137296/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 可达性通知多个警报