iosswiftreachabilityreachability-swift

Notify user reachability (Ashley Mills' Reachability)


I made an app, I want to add function that notify user when app's internet reachability is changed.

I use Ashley Mills' Reachability.swift file.

now I understand how it works, So I put code that when internet reachability is changed, it will print it's status in appDelegate.

However when I tried to put in function that alert user there isn't internet connection, It gets an error.

here is my code in app delegate.

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

var reachability : Reachability?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
do {
        let reachability = try Reachability.reachabilityForInternetConnection()
        self.reachability = reachability
    } catch ReachabilityError.FailedToCreateWithAddress(let address) {

    }
    catch {}

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
    do {
        try reachability?.startNotifier()
    } catch {}

    return true
}

func reachabilityChanged(notification: NSNotification) {
    let reachability = notification.object as! Reachability

    if reachability.isReachable() {

        print("reached")
    } else {


        print("not reached")
    }
}

This works well. However the code in Viewcontroller,

class ViewController: UIViewController {
var reachability : Reachability?
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "HeyUserInternetDoesntWork", name: ReachabilityChangedNotification, object: nil)
    //get call from appDelegate Notification.
     }


func HeyUserInternetDoesntWork() {


    if reachability!.isReachable() {

        print("notify User working")
    } else {


        print("Notify user not working")
    }
}

unexpectedly found nil while unwrapping an Optional value

It gets this error.

I am going to put code for alerting user after it works.

Question here,

How Can I make it this work? It doesn't have to be use that method, but I want to keep using NSNotification. Actually I am a new guy for coding, So please explain details.


Solution

  • Where do you init reachability property? this variable is always nil. In func HeyUserInternetDoesntWork you try to use reachability and of course it gets error. You need to init property like this:

    private let reachability = Reachability.reachabilityForInternetConnection()
    

    After use func HeyUserInternetDoesntWork with 'dynamic' keyword like this:

    dynamic func HeyUserInternetDoesntWork() {
    
    if reachability!.isReachable() {
    
        print("notify User working")
    } else {
    
    
        print("Notify user not working")
    }
    }
    

    Because NSNotificationCenter observer selector should be dynamic.