【发布时间】:2015-01-18 14:45:27
【问题描述】:
当网络状态发生变化时,我需要得到通知。我已经在other topic 中找到了如何检查网络可达性。
有没有我可以使用的委托?
【问题讨论】:
标签: ios swift network-programming
当网络状态发生变化时,我需要得到通知。我已经在other topic 中找到了如何检查网络可达性。
有没有我可以使用的委托?
【问题讨论】:
标签: ios swift network-programming
假设您正在使用来自 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);
请参阅 NSNotificationCenter 和 NSNotification 文档了解如何设置和拆除通知处理程序。
【讨论】: