2014-11-20 108 views

回答

1

假设您使用Apple's Reachability sample project中的代码,您需要注册kReachabilityChangedNotification通知。

假设你有一个有方法的UIViewController子类:

func handleReachabilityChanged(notification:NSNotification) 
{ 
    // notification.object will be a 'Reachability' object that you can query 
    // for the network status. 

    NSLog("Network reachability has changed."); 
} 

然后,你要为你的UIViewController的viewDidLoad()方法通知这样注册:

let nc = NSNotificationCenter.defaultCenter(); 
nc.addObserver(self, selector:"handleReachabilityChanged:", name:kReachabilityChangedNotification, object:nil); 

NSNotificationCenterNSNotification有关如何设置和拆卸通知处理程序的文档。