-
Notifications
You must be signed in to change notification settings - Fork 0
/
LXKReachability.swift
226 lines (176 loc) · 8.42 KB
/
LXKReachability.swift
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// LXKReachability.swift
// Playground
//
// Created by 李现科 on 15/12/28.
// Copyright © 2015年 李现科. All rights reserved.
//
import Foundation
import SystemConfiguration
import CoreTelephony
enum NetworkStatus {
case NotReachable
case ReachableViaWiFi
case ReachableViaWWAN
case ReachableVia2G
case ReachableVia3G
case ReachableVia4G
}
public let kReachabilityChangedNotification = "kNetworkReachabilityChangedNotification"
class LXKReachability {
private var alwaysReturnLocalWiFiStatus = false
private var reachabilityRef = SCNetworkReachabilityRef?()
private var reachabilityCallback: SCNetworkReachabilityCallBack = { (rachability: SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) -> Void in
let lxkReachability = unsafeBitCast(info, UnsafeMutablePointer<LXKReachability>.self).memory
NSNotificationCenter.defaultCenter().postNotificationName(kReachabilityChangedNotification, object: lxkReachability)
}
//MARK: - Instance methods
class func reachabilityWithHostName(hostName: String) -> LXKReachability? {
var result: LXKReachability?
let reachability = SCNetworkReachabilityCreateWithName(nil, (hostName as NSString).UTF8String)
if reachability != nil {
result = LXKReachability()
if result != nil {
result?.alwaysReturnLocalWiFiStatus = false
result?.reachabilityRef = reachability
}
}
return result
}
class func reachabilityWithAddress(hostAddress: sockaddr_in) -> LXKReachability? {
var result: LXKReachability?
var bytes: [Int8] = [Int8(hostAddress.sin_port>>8),Int8(hostAddress.sin_port),Int8(hostAddress.sin_addr.s_addr>>24),Int8(hostAddress.sin_addr.s_addr>>16),Int8(hostAddress.sin_addr.s_addr>>8),Int8(hostAddress.sin_addr.s_addr),hostAddress.sin_zero.0,hostAddress.sin_zero.1,hostAddress.sin_zero.2,hostAddress.sin_zero.3,hostAddress.sin_zero.4,hostAddress.sin_zero.5,hostAddress.sin_zero.6,hostAddress.sin_zero.7]
let data = (bytes[0],bytes[1],bytes[2],bytes[3],bytes[4],bytes[5],bytes[6],bytes[7],bytes[8],bytes[9],bytes[10],bytes[11],bytes[12],bytes[13])
var tmp = sockaddr(sa_len: hostAddress.sin_len, sa_family: hostAddress.sin_family, sa_data: data)
let reachability = withUnsafePointer(&tmp) { (prt: UnsafePointer<sockaddr>) -> SCNetworkReachability? in
return SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, prt)
}
if reachability != nil {
result = LXKReachability()
if result != nil {
result?.alwaysReturnLocalWiFiStatus = false
result?.reachabilityRef = reachability
}
}
return result
}
class func reachabilityForInternetConnection() -> LXKReachability? {
var zeroAddress = sockaddr_in()
bzero(&zeroAddress, sizeof(sockaddr_in))
zeroAddress.sin_len = UInt8(sizeof(sockaddr_in))
zeroAddress.sin_family = sa_family_t(AF_INET)
return self.reachabilityWithAddress(zeroAddress)
}
class func reachabilityForLocalWiFi() -> LXKReachability? {
var result: LXKReachability?
var localWifiAddress = sockaddr_in()
bzero(&localWifiAddress, sizeof(sockaddr_in))
localWifiAddress.sin_len = UInt8(sizeof(sockaddr_in))
localWifiAddress.sin_family = UInt8(AF_INET)
localWifiAddress.sin_addr.s_addr = CFSwapInt32BigToHost(0xA9FE0000)
result = self.reachabilityWithAddress(localWifiAddress)
if result != nil {
result?.alwaysReturnLocalWiFiStatus = true
}
return result
}
//MARK: - Start and stop Notifier
func startNotifier() -> Bool {
var result = false
var context = SCNetworkReachabilityContext(version: 0, info: UnsafeMutablePointer(unsafeAddressOf(self)), retain: nil, release: nil, copyDescription: nil)
withUnsafeMutablePointer(&context) { (prt: UnsafeMutablePointer<SCNetworkReachabilityContext> ) -> Void in
if SCNetworkReachabilitySetCallback(reachabilityRef!, reachabilityCallback, prt) {
if SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef!, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode) {
result = true
}
}
}
return result
}
func stopNotifier() {
if reachabilityRef != nil {
SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef!, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)
}
}
deinit {
stopNotifier()
}
//MARK: - Network flag handler
private func localWiFiStatusForFlags(flags: SCNetworkReachabilityFlags) -> NetworkStatus {
var result = NetworkStatus.NotReachable
if flags.rawValue & SCNetworkReachabilityFlags.IsDirect.rawValue != 0 && flags.rawValue & SCNetworkReachabilityFlags.Reachable.rawValue != 0 {
result = .ReachableViaWiFi
}
return result
}
private func networkStatusForFlags(flags: SCNetworkReachabilityFlags) -> NetworkStatus {
var result = NetworkStatus.NotReachable
if flags.rawValue & SCNetworkReachabilityFlags.Reachable.rawValue == 0 {
return .NotReachable
}
if flags.rawValue & SCNetworkReachabilityFlags.ConnectionRequired.rawValue == 0 {
result = .ReachableViaWiFi
}
if flags.rawValue & SCNetworkReachabilityFlags.ConnectionOnDemand.rawValue != 0 || flags.rawValue & SCNetworkReachabilityFlags.ConnectionOnTraffic.rawValue != 0 {
if flags.rawValue & SCNetworkReachabilityFlags.InterventionRequired.rawValue == 0 {
result = .ReachableViaWiFi
}
}
if flags.rawValue & SCNetworkReachabilityFlags.IsWWAN.rawValue == SCNetworkReachabilityFlags.IsWWAN.rawValue {
let info = CTTelephonyNetworkInfo()
let currentRadioAccessTechnology = info.currentRadioAccessTechnology
if currentRadioAccessTechnology != nil {
switch currentRadioAccessTechnology! {
case CTRadioAccessTechnologyLTE:
result = .ReachableVia4G
break
case CTRadioAccessTechnologyEdge:
fallthrough
case CTRadioAccessTechnologyGPRS:
fallthrough
case CTRadioAccessTechnologyCDMA1x:
result = .ReachableVia2G
break
default:
result = .ReachableVia3G
break
}
return result
}
if flags.rawValue & SCNetworkReachabilityFlags.TransientConnection.rawValue == SCNetworkReachabilityFlags.TransientConnection.rawValue {
if flags.rawValue & SCNetworkReachabilityFlags.ConnectionRequired.rawValue == SCNetworkReachabilityFlags.ConnectionRequired.rawValue {
result = .ReachableVia2G
} else {
result = .ReachableVia3G
}
return result
}
result = .ReachableViaWWAN
}
return result
}
func connectionRequired() -> Bool {
var result = false
var flags = SCNetworkReachabilityFlags()
withUnsafeMutablePointer(&flags) { (prt: UnsafeMutablePointer<SCNetworkReachabilityFlags>) -> Void in
if SCNetworkReachabilityGetFlags(reachabilityRef!, prt) {
result = flags.rawValue & SCNetworkReachabilityFlags.ConnectionRequired.rawValue != 0
}
}
return result
}
func currentReachabilityStatus() -> NetworkStatus {
var result = NetworkStatus.NotReachable
var flags = SCNetworkReachabilityFlags()
withUnsafeMutablePointer(&flags) { (prt: UnsafeMutablePointer<SCNetworkReachabilityFlags>) -> Void in
if SCNetworkReachabilityGetFlags(reachabilityRef!, prt) {
if alwaysReturnLocalWiFiStatus {
result = localWiFiStatusForFlags(flags)
} else {
result = networkStatusForFlags(flags)
}
}
}
return result
}
}