Explorar el Código

resolves issue #85

updated Readme so that the notifications example will work.  Otherwise notifications are not triggered when network status is updated.
Fuad Kamal hace 10 años
padre
commit
0cb701f7ed
Se han modificado 1 ficheros con 24 adiciones y 22 borrados
  1. 24 22
      README.md

+ 24 - 22
README.md

@@ -27,7 +27,7 @@ Just drop the **Reachability.swift** file into your project. That's it!
 
     ``` ruby
     use_frameworks!
-    pod 'ReachabilitySwift', git: 'https://github.com/ashleymills/Reachability.swift'
+    pod 'ReachabilitySwift', :git => 'https://github.com/ashleymills/Reachability.swift'
     ```
 
  3. Run `pod install`.
@@ -35,6 +35,9 @@ Just drop the **Reachability.swift** file into your project. That's it!
 [CocoaPods]: https://cocoapods.org
 [CocoaPods Installation]: https://guides.cocoapods.org/using/getting-started.html#getting-started
 
+ 4. In your code import Reachability like so:
+   `import ReachabilitySwift`
+
 ### Carthage
 [Carthage][] is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
 To install Reachability.swift with Carthage:
@@ -106,21 +109,20 @@ reachability.stopNotifier()
 This sample will use `NSNotification`s to notify when the interface has changed. They will be delivered on the **MAIN THREAD**, so you *can* do UI updates from within the function.
 
 ```swift
-let reachability: Reachability
-do {
-    reachability = try Reachability.reachabilityForInternetConnection()
-} catch {
-    print("Unable to create Reachability")
-    return
-}
+//declare this property where it won't go out of scope relative to your listener
+var reachability: Reachability?
 
-NSNotificationCenter.defaultCenter().addObserver(self,
-                                                 selector: "reachabilityChanged:",
-                                                 name: ReachabilityChangedNotification,
-                                                 object: reachability)
+//declare this inside of viewWillAppear
+do {
+      reachability = try Reachability.reachabilityForInternetConnection()
+    } catch {
+      print("Unable to create Reachability")
+      return
+    }
 
-do{
-      try reachability.startNotifier()
+    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:",name: ReachabilityChangedNotification,object: reachability)
+    do{
+      try reachability?.startNotifier()
     }catch{
       print("could not start reachability notifier")
     }
@@ -131,17 +133,17 @@ and
 ```swift
 func reachabilityChanged(note: NSNotification) {
 
-    let reachability = note.object as! Reachability
+  let reachability = note.object as! Reachability
 
-    if reachability.isReachable() {
-        if reachability.isReachableViaWiFi() {
-            print("Reachable via WiFi")
-        } else {
-            print("Reachable via Cellular")
-        }
+  if reachability.isReachable() {
+    if reachability.isReachableViaWiFi() {
+      print("Reachable via WiFi")
     } else {
-        print("Not reachable")
+      print("Reachable via Cellular")
     }
+  } else {
+    print("Network not reachable")
+  }
 }
 ```