我在AppDelegate的应用程序中有这个函数didFinishLaunchingWithOptions。调用代码以显示警报控制器,但没有任何反应。
AFNetworkReachabilityManager.sharedManager().startMonitoring()
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock{(status: AFNetworkReachabilityStatus?) in
switch status!.hashValue{
case AFNetworkReachabilityStatus.NotReachable.hashValue:
println("Not reachable")
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: nil))
AppDelegate.sharedInstance.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
case AFNetworkReachabilityStatus.ReachableViaWiFi.hashValue , AFNetworkReachabilityStatus.ReachableViaWWAN.hashValue :
println("Reachable")
println(AppDelegate.sharedInstance.description)
default:
println("Unknown status")
}
}
我的AppDelegate中也有这个类。
class var sharedInstance: AppDelegate {
struct Static {
static let instance = AppDelegate()
}
return Static.instance
}
答案 0 :(得分:1)
最初没有注意到AppDelegate.sharedInstance
位。这不是您访问应用代理的方式 - 您需要运行整个应用的UIApplication
创建的代理。你的代码应该是:
AFNetworkReachabilityManager.sharedManager().startMonitoring()
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock{
(status: AFNetworkReachabilityStatus?) in
switch status!.hashValue {
case AFNetworkReachabilityStatus.NotReachable.hashValue:
println("Not reachable")
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: nil))
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
case AFNetworkReachabilityStatus.ReachableViaWiFi.hashValue, AFNetworkReachabilityStatus.ReachableViaWWAN.hashValue :
println("Reachable")
println(AppDelegate.sharedInstance.description)
default:
println("Unknown status")
}
}