Переглянути джерело

Update sample code to show hostName usage

Ashley Mills 10 роки тому
батько
коміт
297ff18a96

+ 16 - 0
ReachabilitySample/Base.lproj/Main.storyboard

@@ -25,16 +25,32 @@
                                 <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                 <nil key="highlightedColor"/>
                             </label>
+                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5EE-X2-pKr">
+                                <rect key="frame" x="275.5" y="242" width="49" height="24"/>
+                                <animations/>
+                                <fontDescription key="fontDescription" type="system" pointSize="20"/>
+                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                                <nil key="highlightedColor"/>
+                            </label>
                         </subviews>
                         <animations/>
                         <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                         <constraints>
+                            <constraint firstItem="UBQ-2c-x8L" firstAttribute="top" secondItem="5EE-X2-pKr" secondAttribute="bottom" constant="8" symbolic="YES" id="CuO-ho-b10"/>
                             <constraint firstAttribute="centerY" secondItem="UBQ-2c-x8L" secondAttribute="centerY" id="K7q-cv-Lfi"/>
+                            <constraint firstItem="5EE-X2-pKr" firstAttribute="centerX" secondItem="UBQ-2c-x8L" secondAttribute="centerX" id="MZr-wz-ELn"/>
                             <constraint firstItem="UBQ-2c-x8L" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="20" id="YBI-sa-7Z9"/>
+                            <constraint firstItem="5EE-X2-pKr" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="225" id="pMO-lY-Wq8"/>
                             <constraint firstAttribute="trailingMargin" secondItem="UBQ-2c-x8L" secondAttribute="trailing" constant="20" id="xcC-HE-ZUz"/>
                         </constraints>
+                        <variation key="default">
+                            <mask key="constraints">
+                                <exclude reference="pMO-lY-Wq8"/>
+                            </mask>
+                        </variation>
                     </view>
                     <connections>
+                        <outlet property="hostNameLabel" destination="5EE-X2-pKr" id="qt8-ZE-mUg"/>
                         <outlet property="networkStatus" destination="UBQ-2c-x8L" id="vop-Uf-5Ev"/>
                     </connections>
                 </viewController>

+ 29 - 10
ReachabilitySample/ViewController.swift

@@ -9,19 +9,34 @@
 import UIKit
 import Reachability
 
-let useClosures = false
-
 class ViewController: UIViewController {
 
     @IBOutlet weak var networkStatus: UILabel!
+    @IBOutlet weak var hostNameLabel: UILabel!
     
     var reachability: Reachability?
     
     override func viewDidLoad() {
         super.viewDidLoad()
 
+        // Start reachability without a hostname intially
+        setupReachability(useHostName: false, useClosures: true)
+        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()) {
+            self.stopNotifier()
+            self.setupReachability(useHostName: true, useClosures: true)
+            self.startNotifier()
+        }
+    }
+    
+    func setupReachability(useHostName useHostName: Bool, useClosures: Bool) {
+        let hostName = "google.com"
+        hostNameLabel.text = useHostName ? hostName : "No host name"
         do {
-            let reachability = try Reachability.reachabilityForInternetConnection()
+            let reachability = try useHostName ? Reachability(hostname: hostName) : Reachability.reachabilityForInternetConnection()
             self.reachability = reachability
         } catch ReachabilityError.FailedToCreateWithAddress(let address) {
             networkStatus.textColor = UIColor.redColor()
@@ -39,7 +54,9 @@ class ViewController: UIViewController {
         } else {
             NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
         }
-        
+    }
+    
+    func startNotifier() {
         do {
             try reachability?.startNotifier()
         } catch {
@@ -49,13 +66,10 @@ class ViewController: UIViewController {
         }
     }
     
-    deinit {
-
+    func stopNotifier() {
         reachability?.stopNotifier()
-        
-        if (!useClosures) {
-            NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
-        }
+        NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
+        reachability = nil
     }
     
     func updateLabelColourWhenReachable(reachability: Reachability) {
@@ -87,6 +101,11 @@ class ViewController: UIViewController {
             updateLabelColourWhenNotReachable(reachability)
         }
     }
+    
+    deinit {
+        stopNotifier()
+    }
+
 }