【问题标题】:Notification of network status changes网络状态变化通知
【发布时间】:2015-01-18 14:45:27
【问题描述】:

当网络状态发生变化时,我需要得到通知。我已经在other topic 中找到了如何检查网络可达性。

有没有我可以使用的委托?

【问题讨论】:

    标签: ios swift network-programming


    【解决方案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 文档了解如何设置和拆除通知处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-08
      • 2013-10-20
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 2019-11-29
      • 2015-08-27
      • 1970-01-01
      相关资源
      最近更新 更多