0
這是在AppDelegate中收到推送通知後通知UIViewController的正確方法嗎? UIViewController需要在獲得推送通知後刷新其內容。 Swift新手如此想確認這是Swift中的一種有效方法。推送通知收到時使用協議更新UIViewController?
的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