Trip Phillips
Trip Phillips

Reputation: 440

Reachability error when switching from Wifi to Cellular in Swift

Hi I am having an issue where when I change my connection from wifi to cellular or vice versa my app crashes instead of showing the "No Connection" view controller like I have programmed it to. I get an error like this:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Spotter.AppDelegate checkReachability]: unrecognized selector sent to instance 0x17404b1f0'

I am using the Objective-C sample provided by Apple and bridging it with my Swift code by the way.

Here is my code:

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {

var window: UIWindow?
var reachability : Reachability?



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

    self.reachability = Reachability.reachabilityForInternetConnection();

    self.reachability!.startNotifier();

return true

}

func checkReachability(notification:NSNotification)
{

    var remoteHostStatus = self.reachability!.currentReachabilityStatus()

    if (remoteHostStatus.value == NotReachable.value)

    {

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let offlineview = mainStoryboard.instantiateViewControllerWithIdentifier("OfflineViewController") as! OfflineViewController
        window!.rootViewController = offlineview
        window!.makeKeyAndVisible()



    }else{

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBar") as! UITabBarController

        window!.rootViewController = initialViewController
        window!.makeKeyAndVisible()


    }
}

Can anyone tell me how I can fix this issue? Thanks

Upvotes: 1

Views: 662

Answers (1)

rmaddy
rmaddy

Reputation: 318814

Your selector is missing the colon in the name. It should be:

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

Upvotes: 2

Related Questions