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

How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?

Swift 2.0 Pass info using userInfo which is a optional Dictionary of type [NSObject : AnyObject]? let imageDataDict:[String: UIImage] = [“image”: image] // Post a notification NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil, userInfo: imageDataDict) // Register to receive notification in your class NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: notificationName, object: nil) // handle notification func showSpinningWheel(notification: NSNotification) { if let … Read more

How to receive NSNotifications from UIWebView embedded YouTube video playback

There are no documented notifications sent by the UIWebView embedded movie player. In fact, the closed implementation used within the UIWebView does differ from the public MPMoviePlayerController in many aspects (e.g. DRM). The most important classes used for playing video content within that UIWebView are called MPAVController and UIMoviePlayerController. The latter one makes the player … Read more

Move view with keyboard using Swift

Here is a solution, without handling the switch from one textField to another: override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillShow:”), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillHide:”), name: UIKeyboardWillHideNotification, object: nil) } func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() { self.view.frame.origin.y -= keyboardSize.height } } func keyboardWillHide(notification: NSNotification) { self.view.frame.origin.y … Read more

NSNotificationCenter addObserver in Swift

Swift 4.0 & Xcode 9.0+: Send(Post) Notification: NotificationCenter.default.post(name: Notification.Name(“NotificationIdentifier”), object: nil) OR NotificationCenter.default.post(name: Notification.Name(“NotificationIdentifier”), object: nil, userInfo: [“Renish”:”Dadhaniya”]) Receive(Get) Notification: NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name(“NotificationIdentifier”), object: nil) Function-Method handler for received Notification: @objc func methodOfReceivedNotification(notification: Notification) {} Swift 3.0 & Xcode 8.0+: Send(Post) Notification: NotificationCenter.default.post(name: Notification.Name(“NotificationIdentifier”), object: nil) Receive(Get) Notification: NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(notification:)), name: Notification.Name(“NotificationIdentifier”), … Read more

Send and receive messages through NSNotificationCenter in Objective-C?

@implementation TestClass – (void) dealloc { // If you don’t remove yourself as an observer, the Notification Center // will continue to try and send notification objects to the deallocated // object. [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } – (id) init { self = [super init]; if (!self) return nil; // Add this instance of … Read more