Procházet zdrojové kódy

Update sample app to test invalid host

Ashley Mills před 10 roky
rodič
revize
67db6370cc
1 změnil soubory, kde provedl 12 přidání a 6 odebrání
  1. 12 6
      ReachabilitySample/ViewController.swift

+ 12 - 6
ReachabilitySample/ViewController.swift

@@ -20,26 +20,32 @@ class ViewController: UIViewController {
         super.viewDidLoad()
         super.viewDidLoad()
 
 
         // Start reachability without a hostname intially
         // Start reachability without a hostname intially
-        setupReachability(useHostName: false, useClosures: true)
+        setupReachability(hostName: nil, useClosures: true)
         startNotifier()
         startNotifier()
 
 
         // After 5 seconds, stop and re-start reachability, this time using a hostname
         // 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))
         let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
         dispatch_after(dispatchTime, dispatch_get_main_queue()) {
         dispatch_after(dispatchTime, dispatch_get_main_queue()) {
             self.stopNotifier()
             self.stopNotifier()
-            self.setupReachability(useHostName: true, useClosures: true)
+            self.setupReachability(hostName: "google.com", useClosures: true)
             self.startNotifier()
             self.startNotifier()
+
+            let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
+            dispatch_after(dispatchTime, dispatch_get_main_queue()) {
+                self.stopNotifier()
+                self.setupReachability(hostName: "invalidhost", useClosures: true)
+                self.startNotifier()            }
+
         }
         }
     }
     }
     
     
-    func setupReachability(useHostName useHostName: Bool, useClosures: Bool) {
-        let hostName = "googledcom"
-        hostNameLabel.text = useHostName ? hostName : "No host name"
+    func setupReachability(hostName hostName: String?, useClosures: Bool) {
+        hostNameLabel.text = hostName != nil ? hostName : "No host name"
         
         
         print("--- set up with host name: \(hostNameLabel.text!)")
         print("--- set up with host name: \(hostNameLabel.text!)")
 
 
         do {
         do {
-            let reachability = try useHostName ? Reachability(hostname: hostName) : Reachability.reachabilityForInternetConnection()
+            let reachability = try hostName == nil ? Reachability.reachabilityForInternetConnection() : Reachability(hostname: hostName!)
             self.reachability = reachability
             self.reachability = reachability
         } catch ReachabilityError.FailedToCreateWithAddress(let address) {
         } catch ReachabilityError.FailedToCreateWithAddress(let address) {
             networkStatus.textColor = UIColor.redColor()
             networkStatus.textColor = UIColor.redColor()