Check for internet connection with Swift

前端 未结 21 1807
别跟我提以往
别跟我提以往 2020-11-22 05:59

When I try to check for an internet connection on my iPhone I get a bunch of errors. Can anyone help me to fix this?

The code:

import Foundation
impo         


        
21条回答
  •  青春惊慌失措
    2020-11-22 06:05

    If someone is already using Alamofire then -

    struct Connectivity {
      static let sharedInstance = NetworkReachabilityManager()!
      static var isConnectedToInternet:Bool {
          return self.sharedInstance.isReachable
        }
    }
    

    Usage:

    if Connectivity.isConnectedToInternet {
         print("Connected")
     } else {
         print("No Internet")
    }
    

提交回复
热议问题