-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkNotfier.swift
124 lines (101 loc) · 3.52 KB
/
NetworkNotfier.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
//
// NetworkNotifier.swift
// E-App
//
// Created by Alex Smith on 2/17/16.
// Copyright © 2016 Alex Smith. All rights reserved.
// See LICENSE for licensing information
//
import Foundation
class NetworkNotifier:NSObject{
let kNetworkNotifierNotification = "kNetworkNotifierNotificationKey"
var remoteHostName:String!
private var _currentStatus:NetworkStatus!
var currentStatus:NetworkStatus!{
get{
return _currentStatus
}set(newStatus){
/* Check if it is the same status as previous */
if newStatus == _currentStatus{
return
}
/* The status is not the same */
_currentStatus = newStatus
/* Post notifications */
networkStatusChanged()
}
}
private var notifierEnabled:Bool = false
private var reachables:[Reachability]!
init(withHost host:String) {
/* Call the super */
super.init()
/* Initialize the array */
reachables = [Reachability]()
remoteHostName = host
/* Setup Notifiers */
setupNotifiers()
}
deinit {
/* Remove the notifiers */
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func startNotifier(){
notifierEnabled = true
}
func stopNotifier(){
notifierEnabled = false
}
func setupNotifiers(){
/* Create the notifcations for reachability */
NSNotificationCenter.defaultCenter().addObserver(self, selector:"reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)
/* Remote Host */
let hostReachability:Reachability = Reachability(hostName: remoteHostName)
hostReachability.startNotifier()
reachables.append(hostReachability)
// /* Internet Connection */
// let internetReachability:Reachability = Reachability.reachabilityForInternetConnection()
// internetReachability.startNotifier()
// reachables.append(internetReachability)
//
// /* Wifi */
// let wifiReachability = Reachability.reachabilityForLocalWiFi()
// wifiReachability.startNotifier()
// reachables.append(wifiReachability)
}
func reachabilityChanged(notification: NSNotification) {
/* Grab the reachability object */
if let reachability = notification.object as? Reachability{
/* Perform updates on the UI */
let networkStatus = reachability.currentReachabilityStatus()
/* Set the newtwork Status */
currentStatus = networkStatus
}
}
func networkStatusChanged(){
/* Ensure that have the notification post enabled */
if !notifierEnabled{
return
}
/* Update the status */
switch(currentStatus){
case NotReachable:
print("not reachable")
break
case ReachableViaWiFi:
print("wifi")
break
case ReachableViaWWAN:
print("wan")
break
default:
break
}
/* Create the user dictionary */
let userInfo:Dictionary<String, AnyObject> = [
"networkStatus": currentStatus.rawValue
]
/* Post the notification */
NSNotificationCenter.defaultCenter().postNotificationName(kNetworkNotifierNotification, object: nil, userInfo: userInfo)
}
}