【发布时间】:2016-10-12 23:57:51
【问题描述】:
我在我的代码中使用 Apple 的可达性类。下面是appdelegate的代码
NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkForReachability:", name: kReachabilityChangedNotification, object: nil);
self.reachability = Reachability.reachabilityForInternetConnection();
self.reachability.startNotifier();
这就是 checkForReachability
func checkForReachability(notification:NSNotification){
let remoteHostStatus = self.reachability!.currentReachabilityStatus()
if (remoteHostStatus.rawValue == NotReachable.rawValue) {
print("NOT REACHABLE")
let myAlert = UIAlertController(title: "Alert", message: "Please check your internet connection.", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title:"Try again.", style:UIAlertActionStyle.Default) {
action in
self.checkForReachability(notification)
}
myAlert.addAction(okAction)
self.window?.rootViewController?.presentViewController(myAlert, animated:true, completion:nil)
}
}
但是在没有网络的情况下(当我关闭wifi时)它不会弹出警报
【问题讨论】:
-
您是否在控制台中收到打印语句(无法访问)?
标签: ios swift reachability