Bumped by Community user
Creating SwiftRechabilitySwiftReachability Class in Swift 2.2
Creating SwiftRechability Class in Swift 2.2
I am novice at creating independent classes for user. I have seen many Reachability
library for Swift, but I need to create my own class for that. Here is my code:
class SwiftReachability: NSObject {
let REACHABILITY_NOTIFIER_KEY = "reachability_notifier_key"
var backgroundQueue: NSOperationQueue?
var isCancelled: Bool = false
//MARK: - Checking Internet Connecion
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(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer(0ドル))
}
var flags = SCNetworkReachabilityFlags(rawValue: 0)
SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags)
#if DEBUG
print("Reachability flags = \(flags.rawValue) uint = \(UInt32(kSCNetworkFlagsReachable))")
#endif
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection) ? true : false
}
func startNotifier(){
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SwiftReachability.networkConnectionObserver), name: REACHABILITY_NOTIFIER_KEY, object: nil)
callingBackgroungThread()
}
func stopNotifier(){
NSNotificationCenter.defaultCenter().removeObserver(self, name: REACHABILITY_NOTIFIER_KEY, object: nil)
backgroundQueue?.cancelAllOperations()
isCancelled = true
}
func callingBackgroungThread(){
backgroundQueue = NSOperationQueue()
let operation = NSBlockOperation {
if self.isConnectedToNetwork(){
NSNotificationCenter.defaultCenter().postNotificationName(self.REACHABILITY_NOTIFIER_KEY, object:nil)
}
}
operation.completionBlock = {
if !self.isCancelled{
self.callingBackgroungThread()
}
}
delay(5.0, closure: {
if !self.isCancelled{
self.backgroundQueue?.addOperation(operation)
}
})
}
func networkConnectionObserver(){
print("Observer")
showAlert("Network Identified")
}
//MARK: Dispatch Asyn Queue After
func delay(delay:Double, closure:()->()) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}
func showAlert(message: String){
let alertController = UIAlertController(title: "Bing", message: message, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
self.stopNotifier()
})
alertController.addAction(okAction)
dispatch_async(dispatch_get_main_queue(), {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController!.presentViewController(alertController, animated: true, completion: nil)
})
}
}
Can anyone review it to make it better?
default