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

3

23

Is there an easy-to-grock pattern how to send a NSNotification (Objective C) | Notification (in Swift) and how to receive one? Code snippet? The docs write like 150 pages on the topic. Would like to see a quick example.

Piece answered 20/4, 2010 at 15:28 Comment(0)
O
73

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];
Objective answered 20/4, 2010 at 16:52 Comment(1)
- (void)cacheUpdated:(NSNotification *)notification { [self load]; }Kingfish
B
2

Swift Version for the same :

Whenever you need to post the notification :

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UpdateAccepted"), object: self)

On controller where you want to receive the notification :

override func viewDidLoad(_ animated: Bool) {
    super.viewDidLoad(true) {
    NotificationCenter.default.addObserver(self, selector: #selector(updateAccepted(notification:)), name: NSNotification.Name(rawValue: "UpdateAccepted"), object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "UpdateAccepted"), object: nil)
}

@objc func updateAccepted(notification: Notification) {
    handleRefresh()
}
Berardo answered 29/6, 2019 at 14:15 Comment(0)
E
-2
  1. Register for a push notification profile and set it up in your application heres a link to do that PushNotification (Note you will need some server or something capturing device push notificaiton IDs in order to be able to send notification to those devices)

  2. Next assuming you are using a Windows Server or something .net compatible as your server there is a nice C# api written to send push notifications to the apple server (once you have a certificate and devices registered for it which you have stored in your server ), there are demos in here of how to use it, pretty cool heres a link C# push notification src

Thats pretty much it...i gave u a quick solution using a .Net technology, if you are using somethign else you can browse around to see if there are solutions available in the platform you are using im sure youll find something, if not you can alwyas make your own :)

Easy answered 20/4, 2010 at 15:54 Comment(5)
He didn't ask about push notifications. Of course, he may have meant to, but that's not what he said :-)Objective
@Paul you are right - still I think Daniel has chosen the right answer as I would really wonder how an App should receive notifications other than through Push Notifications. --- Just reading your answer, well yes, that certainly is a notification as well :)Thales
Notifications are a common idiom in iPhone programming. See answer below.Objective
I did mean notifications from NSNotificationCenter ;-) but nice to know how push notifications work. Thanks!Piece
Yes the question is about notification centre not for push notification.Bloodshed

© 2022 - 2024 — McMap. All rights reserved.