2016-11-11 91 views
1

我試過檢查在斯威夫特3互聯網連接,但代碼不適合我。如何檢查在Swift 3或Swift 4中的Internet連接?

class func isConnectedToNetwork() -> Bool { 

    var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)) 
    zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) 
    zeroAddress.sin_family = sa_family_t(AF_INET) 

    let defaultRouteReachability = withUnsafePointer(&zeroAddress) { 
     SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue() 
    } 

    var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: UInt32(0)) 
    if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 { 
     return false 
    } 

    let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0 
    let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0 

    return (isReachable && !needsConnection) ? true : false 
    } 

我還引進了SystemConfiguration框架。請建議如何檢查。

+1

定義 「不工作」。 – rmaddy

+0

您可以查看此鏈接以供參考。它包含連接或斷開連接時的網絡檢查。 http://stackoverflow.com/questions/40066530/my-reachability-notifier-is-only-able-to-be-called-once/40068225#40068225 –

回答

0
func isConnectedToNetwork() -> Bool { 
    var zeroAddress = sockaddr_in() 
    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress)) 
    zeroAddress.sin_family = sa_family_t(AF_INET) 
    let defaultRouteReachability = withUnsafePointer(&zeroAddress) { 
     SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)) 
    } 
    var flags = SCNetworkReachabilityFlags() 
    if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) { 
     return false 
    } 
    let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0 
    let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0 
    return (isReachable && !needsConnection) 
} 

problemstucks.com

+1

如果你連接到一個Wi-Fi但不能夠連接到互聯網(打開谷歌頁面),它仍然返回true爲可及。這是不對的。 –

0

最新swift3代碼

這裏最簡單的檢查互聯網連接的方式是使用iOS可達API。檢查這個波紋管鏈接並下載添加可達文件到您的項目。你必須和應該加入systemconfiguration框架添加到項目

[1]: https://github.com/ashleymills/Reachability.swift/tree/feature/ios10 

上述工作完成後只需編寫代碼來檢查網絡connection.here我寫一些警報信息也。

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: "Check", message: "Internet Connection is Required at first time running the app to load images and videos ", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil)) 

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


     print("Not connected") 
    case .online(.wwan): 
     print("Connected via WWAN") 
    case .online(.wiFi): 
     print("Connected via WiFi") 
    } 


} 
0

在Alamofire

的情況下
import Alamofire 

//添加在您的viewDidLoad(),或在AppDelegate中這樣

let reachabilityManager = NetworkReachabilityManager() 
    reachabilityManager.listener = { status in 

     switch status { 

     case .notReachable: 
      print("The network is not reachable") 
      self.onInternetDisconnection() 

     case .unknown : 
      print("It is unknown whether the network is reachable") 
      self.onInternetDisconnection() // not sure what to do for this case 

     case .reachable(.ethernetOrWiFi): 
      print("The network is reachable over the WiFi connection") 
      self.onInternetConnection() 

     case .reachable(.wwan): 
      print("The network is reachable over the WWAN connection") 
      self.onInternetConnection() 

     } 
    }