1

I have imported Reachability.h .m files like written here into my Swift project, but observer / event handler will not get called after start, why?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)

    let hostReachability = Reachability(hostName: "www.apple.com");
    hostReachability.startNotifier();

    return true
}

func reachabilityChanged(note: NSNotification) { // <- DOES NOT GET CALLED

    let reachability: Reachability = note.object as Reachability;

    if(reachability.currentReachabilityStatus() != .NotReachable) {

    }
}
Community
  • 1
  • 1
János
  • 32,867
  • 38
  • 193
  • 353

3 Answers3

12

The Reachability object returned is an autoreleased object , so it has deallocated hence notification is not triggering . You can fix by keeping a strong reference to it.

create a property to hold Reachability object

var reachability:Reachability?

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        // Override point for customization after application launch.

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name:kReachabilityChangedNotification, object: nil)

        reachability = Reachability(hostName: "www.apple.com");
        reachability?.startNotifier();

        return true
    }

This will fix your problem.

The observer's name is changed from ReachabilityChangedNotification to kReachabilityChangedNotification. Notice the "k"

Nouras
  • 129
  • 2
  • 12
Yatheesha
  • 10,412
  • 5
  • 42
  • 45
0

As mentioned in other answers, you need to maintain a strong reference to the Reachability object. If you aren't checking a particular host, then call reachabilityForInternetConnection:

let internetReachability = Reachability.reachabilityForInternetConnection()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Override point for customization after application launch.

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: internetReachability)
    internetReachability.startNotifier()
    let hasInternet = internetReachability.currentReachabilityStatus().value == 0 ? false : true

    // Save to user defaults, log to console, etc.

    return true
}

func reachabilityChanged(note: NSNotification) {
    var hasInternet = Bool()
    if let reachability = note.object as? Reachability {
        hasInternet = internetReachability.currentReachabilityStatus().value == 0 ? false : true
    }

    // Update user defaults, log to console, show error message, etc.
}
Albert Lardizabal
  • 6,548
  • 7
  • 34
  • 34
0

For iOS 3+, use below code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    NotificationCenter.default.addObserver(self, selector: #selector(checkForReachability(notification:)), name: NSNotification.Name.reachabilityChanged, object: nil)
    reachability = Reachability.forInternetConnection()
    reachability.startNotifier()

    return true
}

 // MARK:
// MARK: Check Internet Status
func checkForReachability(notification:NSNotification)
{
    let networkReachability = notification.object as! Reachability;
    let remoteHostStatus = networkReachability.currentReachabilityStatus()

    if (remoteHostStatus == NotReachable)
    {
        print("Not Reachable")
    }
    else
    {
        print("Reachable")
    }
}
Ankit Goyal
  • 3,019
  • 1
  • 21
  • 26