Tag: swn nsnotificationcenter

在Swift中使用Reachability,NSNotification和Network Link Conditioner检测networking连接变化

我正在尝试将networking连接检测function集成到我的应用程序中,但是似乎在某些地方我犯了一个错误,因为我的networking更改没有被检测/打印到控制台中。 正如在文章中提到的,我目前正在使用以下这些类和工具来完成这项工作: 可达性{.h, .m} NSNotificationCenter networking链接调节器 码 在AppDelegate.Swift中 ,我设置了NSNotificationCenter来检测更改: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // … // A: Checks if the device is connected to the internet var defaultCenter: Void = NSNotificationCenter().addObserver(self, selector:"checkForReachability", name: kReachabilityChangedNotification, object: nil) } 在同一个AppDelegate类中,我也创build了这个函数,在有变化时触发: func checkForReachability () { var networkReachability = Reachability.reachabilityForInternetConnection() networkReachability.startNotifier() var remoteHostStatus […]

NSNotificationCenter addObserver在Swift中

你如何将Swift中的观察者添加到默认通知中心? 我试图移植这个代码,当电池电量发生变化时发送通知。 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];