如何发布和接收NSnotification(目标C)|通知(迅速)?
-
28-09-2019 - |
题
是否有易于抓紧的模式如何发送NSNOTIFIENCT(目标C)|通知(Swift)以及如何接收?代码片段?文档在该主题上像150页一样写。想看看一个快速的例子。
解决方案
发送通知:
[[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];
其他提示
相同的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()
}
注册推送通知配置文件,并将其设置在您的应用程序中 推送通知 (请注意,您将需要一些服务器或捕获设备推送Notificaiton ID,以便能够将通知发送到这些设备)
接下来,假设您使用的是Windows Server或.NET兼容的服务器作为服务器,则编写了一个不错的C#API,可以将推送通知发送到Apple Server(一旦您将其存储在服务器中存储的证书和设备注册后) ,这里有一些演示如何使用它,这很酷的Heres链接 C#推送通知SRC
多数民众赞成在它...我使用.NET技术给UA快速解决方案,如果您使用someThign else,您可以浏览以查看平台中是否有解决方案,您正在使用IM确定您会找到一些东西,如果不是您可以自己制作自己的:)
不隶属于 StackOverflow