Advertisement
Guest User

Untitled

a guest
Oct 11th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.36 KB | None | 0 0
  1. public class Reach {
  2.    
  3.     private var reachability: SCNetworkReachability!
  4.    
  5.     func connectionStatus() -> ReachabilityStatus {
  6.         // ...
  7.         return ReachabilityStatus(reachabilityFlags: flags)
  8.     }
  9.    
  10.     func monitorReachabilityChanges() {
  11.         let host = "google.com"
  12.         var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
  13.         reachability = SCNetworkReachabilityCreateWithName(nil, host)!
  14.        
  15.         SCNetworkReachabilitySetCallback(reachability, callback, &context)
  16.         SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes)
  17.     }
  18.    
  19.     func stopMonitoringReachabilityChanges() {
  20.         SCNetworkReachabilityUnscheduleFromRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes)
  21.     }
  22.    
  23. }
  24.  
  25. func callback(reachability: SCNetworkReachability, flags: SCNetworkReachabilityFlags, pointer: UnsafeMutablePointer<Void>) {
  26.     let obj = Unmanaged<Reach>.fromOpaque(COpaquePointer(pointer)).takeUnretainedValue() // attempt to create an Unmanaged instance from a null pointer
  27.    
  28.     let status = ReachabilityStatus(reachabilityFlags: flags)
  29.    
  30.     NSNotificationCenter.defaultCenter().postNotificationName(ReachabilityStatusChangedNotification, object: nil, userInfo: ["Status": status.description])
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement