Kaynağa Gözat

Updated Reachability iOS to Xcode 8 beta 2 (sample app updated as well)

Rob Goble 9 yıl önce
ebeveyn
işleme
40186783c3

+ 4 - 4
Reachability/Reachability.swift

@@ -44,7 +44,7 @@ func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFl
 
     guard let info = info else { return }
     
-    let reachability = Unmanaged<Reachability>.fromOpaque(OpaquePointer(info)).takeUnretainedValue()
+    let reachability = Unmanaged<Reachability>.fromOpaque(info).takeUnretainedValue()
 
     DispatchQueue.main.async { 
         reachability.reachabilityChanged(flags:flags)
@@ -76,7 +76,7 @@ public class Reachability: NSObject {
     public var whenReachable: NetworkReachable?
     public var whenUnreachable: NetworkUnreachable?
     public var reachableOnWWAN: Bool
-    public var notificationCenter = NotificationCenter.default()
+    public var notificationCenter = NotificationCenter.default
 
     public var currentReachabilityStatus: NetworkStatus {
         if isReachable() {
@@ -147,7 +147,7 @@ public class Reachability: NSObject {
         guard let reachabilityRef = reachabilityRef where !notifierRunning else { return }
         
         var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
-        context.info = UnsafeMutablePointer<Void>(OpaquePointer(bitPattern: Unmanaged<Reachability>.passUnretained(self)))
+        context.info = UnsafeMutablePointer<Void>(Unmanaged<Reachability>.passUnretained(self).toOpaque())
         
         if !SCNetworkReachabilitySetCallback(reachabilityRef, callback, &context) {
             stopNotifier()
@@ -286,7 +286,7 @@ public class Reachability: NSObject {
 
     private func isOnWWAN(flags:SCNetworkReachabilityFlags) -> Bool {
         #if os(iOS)
-            return flags.contains(.iswwan)
+            return flags.contains(.isWWAN)
         #else
             return false
         #endif

+ 6 - 6
ReachabilitySample/AppDelegate.swift

@@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
     var window: UIWindow?
 
 
-    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
+    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
         // Override point for customization after application launch.
         return true
     }
 
-    func applicationWillResignActive(application: UIApplication) {
+    func applicationWillResignActive(_ application: UIApplication) {
         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
         // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     }
 
-    func applicationDidEnterBackground(application: UIApplication) {
+    func applicationDidEnterBackground(_ application: UIApplication) {
         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     }
 
-    func applicationWillEnterForeground(application: UIApplication) {
+    func applicationWillEnterForeground(_ application: UIApplication) {
         // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     }
 
-    func applicationDidBecomeActive(application: UIApplication) {
+    func applicationDidBecomeActive(_ application: UIApplication) {
         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     }
 
-    func applicationWillTerminate(application: UIApplication) {
+    func applicationWillTerminate(_ application: UIApplication) {
         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
     }
 

+ 17 - 17
ReachabilitySample/ViewController.swift

@@ -24,14 +24,14 @@ class ViewController: UIViewController {
         startNotifier()
 
         // After 5 seconds, stop and re-start reachability, this time using a hostname
-        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
-        dispatch_after(dispatchTime, dispatch_get_main_queue()) {
+        let dispatchTime = DispatchTime.now() + Double(Int64(UInt64(5) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
+        DispatchQueue.main.after(when: dispatchTime) {
             self.stopNotifier()
             self.setupReachability(hostName: "google.com", useClosures: true)
             self.startNotifier()
 
-            let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
-            dispatch_after(dispatchTime, dispatch_get_main_queue()) {
+            let dispatchTime = DispatchTime.now() + Double(Int64(UInt64(5) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
+            DispatchQueue.main.after(when: dispatchTime) {
                 self.stopNotifier()
                 self.setupReachability(hostName: "invalidhost", useClosures: true)
                 self.startNotifier()            }
@@ -39,7 +39,7 @@ class ViewController: UIViewController {
         }
     }
     
-    func setupReachability(hostName hostName: String?, useClosures: Bool) {
+    func setupReachability(hostName: String?, useClosures: Bool) {
         hostNameLabel.text = hostName != nil ? hostName : "No host name"
         
         print("--- set up with host name: \(hostNameLabel.text!)")
@@ -48,24 +48,24 @@ class ViewController: UIViewController {
             let reachability = try hostName == nil ? Reachability.reachabilityForInternetConnection() : Reachability(hostname: hostName!)
             self.reachability = reachability
         } catch ReachabilityError.FailedToCreateWithAddress(let address) {
-            networkStatus.textColor = UIColor.redColor()
+            networkStatus.textColor = UIColor.red()
             networkStatus.text = "Unable to create\nReachability with address:\n\(address)"
             return
         } catch {}
         
         if (useClosures) {
             reachability?.whenReachable = { reachability in
-                dispatch_async(dispatch_get_main_queue()) {
+                DispatchQueue.main.async {
                     self.updateLabelColourWhenReachable(reachability)
                 }
             }
             reachability?.whenUnreachable = { reachability in
-                dispatch_async(dispatch_get_main_queue()) {
+                DispatchQueue.main.async {
                     self.updateLabelColourWhenNotReachable(reachability)
                 }
             }
         } else {
-            NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
+            NotificationCenter.default.addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
         }
     }
     
@@ -74,7 +74,7 @@ class ViewController: UIViewController {
         do {
             try reachability?.startNotifier()
         } catch {
-            networkStatus.textColor = UIColor.redColor()
+            networkStatus.textColor = UIColor.red()
             networkStatus.text = "Unable to start\nnotifier"
             return
         }
@@ -83,31 +83,31 @@ class ViewController: UIViewController {
     func stopNotifier() {
         print("--- stop notifier")
         reachability?.stopNotifier()
-        NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
+        NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: nil)
         reachability = nil
     }
     
-    func updateLabelColourWhenReachable(reachability: Reachability) {
+    func updateLabelColourWhenReachable(_ reachability: Reachability) {
         print("\(reachability.description) - \(reachability.currentReachabilityString)")
         if reachability.isReachableViaWiFi() {
-            self.networkStatus.textColor = UIColor.greenColor()
+            self.networkStatus.textColor = UIColor.green()
         } else {
-            self.networkStatus.textColor = UIColor.blueColor()
+            self.networkStatus.textColor = UIColor.blue()
         }
         
         self.networkStatus.text = reachability.currentReachabilityString
     }
 
-    func updateLabelColourWhenNotReachable(reachability: Reachability) {
+    func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
         print("\(reachability.description) - \(reachability.currentReachabilityString)")
 
-        self.networkStatus.textColor = UIColor.redColor()
+        self.networkStatus.textColor = UIColor.red()
         
         self.networkStatus.text = reachability.currentReachabilityString
     }
 
     
-    func reachabilityChanged(note: NSNotification) {
+    func reachabilityChanged(_ note: Notification) {
         let reachability = note.object as! Reachability
         
         if reachability.isReachable() {