forked from conferencesapp/rubyconferences-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReachability.swift
More file actions
34 lines (28 loc) · 942 Bytes
/
Reachability.swift
File metadata and controls
34 lines (28 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// Reachability.swift
//
//
// Created by Rashmi Yadav on 5/16/15.
//
//
import Foundation
import SystemConfiguration
public class Reachability {
public func connectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}) else {
return false
}
var flags : SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.Reachable)
let needsConnection = flags.contains(.ConnectionRequired)
return (isReachable && !needsConnection)
}
}