菜鸟教程小白 发表于 2022-12-11 18:02:06

ios - 如何在 Swift 3 或 Swift 4 中检查互联网连接?


                                            <p><div>
            <aside class="s-notice s-notice__info post-notice js-post-notice mb16"role="status">
      <div class="d-flex fd-column fw-nowrap">
            <div class="d-flex fw-nowrap">
                <div class="flex--item wmn0 fl1 lh-lg">
                  <div class="flex--item fl1 lh-lg">
                        <b>这个问题在这里已经有了答案</b>:
                        
                  <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>最新的 swift3 代码</strong></p>

<p>这里 检查互联网连接的最简单方法是使用 iOS 可达性 API。检查此波纹管链接并下载将可达性文件添加到您的项目。您必须并且应该将系统配置框架添加到项目中</p>

<pre><code> : https://github.com/ashleymills/Reachability.swift/tree/feature/ios10
</code></pre>

<p>完成上述工作后,只需编写此代码以检查网络连接。这里我也写了一些警报消息。</p>

<pre><code>    func networkStatusChanged(_ notification: Notification) {
    let userInfo = (notification as NSNotification).userInfo
    print(userInfo!)
}
func interNetChecking( )   {
    let status = Reach().connectionStatus()


    switch status {
    case .unknown, .offline:

      let alert = UIAlertController(title: &#34;Check&#34;, message: &#34;Internet Connection is Required at first time running the app to load images and videos &#34;, preferredStyle: UIAlertControllerStyle.alert)
      alert.addAction(UIAlertAction(title: &#34;Cancel&#34;, style: UIAlertActionStyle.default, handler: nil))

      DispatchQueue.main.async(execute: {
            self.present(alert, animated: true, completion: nil)
      })


      print(&#34;Not connected&#34;)
    case .online(.wwan):
      print(&#34;Connected via WWAN&#34;)
    case .online(.wiFi):
      print(&#34;Connected via WiFi&#34;)
    }


}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Swift 3 或 Swift 4 中检查互联网连接?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40541141/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40541141/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Swift 3 或 Swift 4 中检查互联网连接?