Explorar o código

Merge pull request #1308 from Alamofire/fix/network_reachability_ipv6_support

Fix - Network Reachability IPv6 Support
Christian Noon %!s(int64=9) %!d(string=hai) anos
pai
achega
3f6845d809

+ 14 - 23
Source/NetworkReachabilityManager.swift

@@ -116,32 +116,23 @@ public class NetworkReachabilityManager {
     }
 
     /**
-        Creates a `NetworkReachabilityManager` instance with the default socket IPv4 or IPv6 address.
+        Creates a `NetworkReachabilityManager` instance that monitors the address 0.0.0.0.
+
+        Reachability treats the 0.0.0.0 address as a special token that causes it to monitor the general routing
+        status of the device, both IPv4 and IPv6.
 
         - returns: The new `NetworkReachabilityManager` instance.
-     */
+    */
     public convenience init?() {
-        if #available(iOS 9.0, OSX 10.10, *) {
-            var address = sockaddr_in6()
-            address.sin6_len = UInt8(sizeofValue(address))
-            address.sin6_family = sa_family_t(AF_INET6)
-
-            guard let reachability = withUnsafePointer(&address, {
-                SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
-            }) else { return nil }
-
-            self.init(reachability: reachability)
-        } else {
-            var address = sockaddr_in()
-            address.sin_len = UInt8(sizeofValue(address))
-            address.sin_family = sa_family_t(AF_INET)
-
-            guard let reachability = withUnsafePointer(&address, {
-                SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
-            }) else { return nil }
-
-            self.init(reachability: reachability)
-        }
+        var address = sockaddr_in()
+        address.sin_len = UInt8(sizeofValue(address))
+        address.sin_family = sa_family_t(AF_INET)
+
+        guard let reachability = withUnsafePointer(&address, {
+            SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
+        }) else { return nil }
+
+        self.init(reachability: reachability)
     }
 
     private init(reachability: SCNetworkReachability) {

+ 8 - 4
Tests/NetworkReachabilityManagerTests.swift

@@ -106,18 +106,22 @@ class NetworkReachabilityManagerTestCase: BaseTestCase {
 
     func testThatHostManagerIsNotifiedWhenStartListeningIsCalled() {
         // Given
-        let manager = NetworkReachabilityManager(host: "localhost")
-        let expectation = expectationWithDescription("listener closure should be executed")
+        guard let manager = NetworkReachabilityManager(host: "store.apple.com") else {
+            XCTFail("manager should NOT be nil")
+            return
+        }
 
+        let expectation = expectationWithDescription("listener closure should be executed")
         var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
 
-        manager?.listener = { status in
+        manager.listener = { status in
+            guard networkReachabilityStatus == nil else { return }
             networkReachabilityStatus = status
             expectation.fulfill()
         }
 
         // When
-        manager?.startListening()
+        manager.startListening()
         waitForExpectationsWithTimeout(timeout, handler: nil)
 
         // Then