NSNotificationCenter Swift 3.0 on keyboard show and hide

Swift 3: override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: Notification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: Notification.Name.UIKeyboardWillHide, object: nil) } func keyboardWillShow(notification: Notification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { print(“notification: Keyboard will show”) if self.view.frame.origin.y == 0{ self.view.frame.origin.y -= keyboardSize.height } } } func keyboardWillHide(notification: Notification) { if let keyboardSize … Read more

How to post and receive an NSNotifications (Objective C) | Notifications (in Swift)?

Send a notification: [[NSNotificationCenter defaultCenter] postNotificationName:@”MyCacheUpdatedNotification” object:self]; Receive it: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheUpdated:) name:@”MyCacheUpdatedNotification” object:nil]; Act on it: – (void)cacheUpdated:(NSNotification *)notification { [self load]; } And dispose of it: [[NSNotificationCenter defaultCenter] removeObserver:self];

How can I listen for all notifications sent to the iOS NSNotificationCenter’s defaultCenter?

Use NSNotificationCenter’s addObserverForName:object:queue:usingBlock: OR addObserver:selector:name:object: method and pass nil for the name and object. Example The following code should do the job: – (void)dumpNotifications { NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter]; [notifyCenter addObserverForName:nil object:nil queue:nil usingBlock:^(NSNotification *notification){ // Explore notification NSLog(@”Notification found with:” “\r\n name: %@” “\r\n object: %@” “\r\n userInfo: %@”, [notification name], [notification object], … Read more

How to pass object with NSNotificationCenter

You’ll have to use the “userInfo” variant and pass a NSDictionary object that contains the messageTotal integer: NSDictionary* userInfo = @{@”total”: @(messageTotal)}; NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; [nc postNotificationName:@”eRXReceived” object:self userInfo:userInfo]; On the receiving end you can access the userInfo dictionary as follows: -(void) receiveTestNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@”TestNotification”]) { NSDictionary* userInfo = notification.userInfo; NSNumber* … Read more

Detecting Network Connectivity Changes using Reachability, NSNotification and Network Link Conditioner in Swift

You must create a Reachability object before you can receive notifications from it. Also, be sure to call the startNotifier() method on the Reachability object you create. This would be an example of how to do so inside of your application delegate: class AppDelegate: UIResponder, UIApplicationDelegate { private var reachability:Reachability!; func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: … Read more