菜鸟教程小白 发表于 2022-12-13 14:26:20

ios - 如何在 iOS 中检查互联网是否处于事件状态(检查网络可用性和网页可访问性)?


                                            <p><p>我目前在一个 iOS 项目中工作,我需要检查我的手机是否连接到互联网(移动数据/wifi)。我正在使用可访问性类,它只检查网络连接的可用性,但假设我的手机没有余额。在这种情况下,我将无法访问互联网,但仍可访问性显示互联网可通过移动网络访问,因为数据连接已打开,但我无法访问该页面。</p>

<p>我该如何解决这个问题?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用 connection:didReceiveResponse: 进行检查</p>

<pre><code>      NSString *urlString=@&#34;yourUrl&#34;;
      NSURL *url=;
      NSURLRequest *request=;
      NSURLConnection *connection=;
      ;

      -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
      {
      NSLog(@&#34;%@&#34;,response);
      ;
      NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
      int code = (int);
      if (code == 200) {
            NSLog(@&#34;File exists&#34;);
      }
      else if(code == 404)
      {
            NSLog(@&#34;File not exist&#34;);
      }
      }
      -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
      {
      NSLog(@&#34;File not exist&#34;);
      }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 iOS 中检查互联网是否处于事件状态(检查网络可用性和网页可访问性)?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40826101/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40826101/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 iOS 中检查互联网是否处于事件状态(检查网络可用性和网页可访问性)?