如何发布和接收 NSNotifications(目标 C) |通知(Swift 中)?

发布于 2024-08-29 22:45:08 字数 99 浏览 6 评论 0原文

是否有一种易于理解的模式如何发送 NSNotification(目标 C)|通知(Swift 中)以及如何接收通知?代码片段?文档关于这个主题写了大约 150 页。想看一个简单的例子。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅语花开 2024-09-05 22:45:08

发送通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyCacheUpdatedNotification" object:self];

接收通知: 采取

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheUpdated:) name:@"MyCacheUpdatedNotification" object:nil];

行动:

- (void)cacheUpdated:(NSNotification *)notification {
[self load];
}

并处理通知:

[[NSNotificationCenter defaultCenter] removeObserver:self];

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];
鸵鸟症 2024-09-05 22:45:08

相同的 Swift 版本:

每当您需要发布通知时:

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

在您想要接收通知的控制器上:

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()
}

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()
}
合约呢 2024-09-05 22:45:08
  1. 注册推送通知配置文件并在您的应用程序中进行设置,这里有一个链接来执行此操作PushNotification(请注意,您将需要一些服务器或捕获设备推送通知 ID 的设备,以便能够向这些设备发送通知)

  2. 接下来假设您使用 Windows Server 或 .net 兼容的东西作为您的服务器,有一个很好的 C# api编写用于向苹果服务器发送推送通知(一旦您拥有存储在服务器中的证书和注册设备),这里有演示如何使用它,非常酷,这里有一个链接 C# 推送通知 src

差不多就是这样...我给了 ua使用.Net技术的快速解决方案,如果您正在使用其他东西,您可以浏览一下,看看您正在使用的平台是否有可用的解决方案,我相信您会找到一些东西,如果没有,您也可以自己制作:)

  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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文