Это правильный способ оповестить UIViewController после получения push-уведомления в AppDelegate? UIViewController должен обновить свое содержимое после получения push-уведомления. Новое для Swift, поэтому хочу подтвердить, что это правильный подход в Swift.Использование протокола для обновления UIViewController при получении push-уведомления?
AppDelegate:
protocol PushNotificationDelegate : class {
func didReceivePushNotification()
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var pushDelegates = [PushNotificationDelegate]()
...
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
// Notify delegates
for delegate in pushDelegates {
delegate.didReceivePushNotification()
}
}
func addPushNotificationDelegate(newDelegate: PushNotificationDelegate) {
if (pushDelegates.indexOf{$0 === newDelegate} == nil) {
pushDelegates.append(newDelegate)
}
}
}
UIViewController:
class HomeViewController: UIViewController, PushNotificationDelegate {
override func viewDidLoad() {
super.viewDidLoad()
...
// Get notified when push notifications come in
if let delegate = UIApplication.sharedApplication().delegate as? AppDelegate {
delegate.addPushNotificationDelegate(self)
}
}
func didReceivePushNotification() {
}
}
ОК спасибо. Но если HomeViewController является корневым контроллером, нет необходимости удалять его из списка делегатов? – Crashalot
Да, в этом случае вам не нужно. Но для других вам нужно. – Shripada