在 Swift 中从 Wifi 切换到蜂窝网络时出现可达性错误
Reachability error when switching from Wifi to Cellular in Swift
您好,我遇到一个问题,当我将连接从 wifi 更改为蜂窝网络或从 wifi 更改为蜂窝网络连接时,我的应用程序崩溃了,而不是像我编程的那样显示 "No Connection" 视图控制器。我收到这样的错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Spotter.AppDelegate checkReachability]: unrecognized selector sent to instance 0x17404b1f0'
我正在使用 Apple 提供的 Objective-C 示例,顺便将它与我的 Swift 代码连接起来。
这是我的代码:
@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()
}
}
谁能告诉我如何解决这个问题?
谢谢
您的选择器名称中缺少冒号。应该是:
NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkReachability:", name: kReachabilityChangedNotification, object: nil);
您好,我遇到一个问题,当我将连接从 wifi 更改为蜂窝网络或从 wifi 更改为蜂窝网络连接时,我的应用程序崩溃了,而不是像我编程的那样显示 "No Connection" 视图控制器。我收到这样的错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Spotter.AppDelegate checkReachability]: unrecognized selector sent to instance 0x17404b1f0'
我正在使用 Apple 提供的 Objective-C 示例,顺便将它与我的 Swift 代码连接起来。
这是我的代码:
@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()
}
}
谁能告诉我如何解决这个问题? 谢谢
您的选择器名称中缺少冒号。应该是:
NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkReachability:", name: kReachabilityChangedNotification, object: nil);