iosswiftnotificationcenter

How to subscribe to PKPassLibraryNotificationName


I need to observe the .PKPassLibraryDidChange and .PKPassLibraryRemotePaymentPassesDidChange notifications that are specified in https://developer.apple.com/documentation/passkit/pkpasslibrarynotificationname

However they are not a subclass of NSNotification.Name so the

let observer = NotificationCenter.default.addObserver(forName: PKPassLibraryNotificationName.PKPassLibraryDidChange, object: nil, queue: nil) { notification in  
     ...  
} 

doesn't compile.

Is there any additional import needed to be able to observe PKPassLibraryNotificationName on the NotificationCenter?


Solution

  • To answer your actual question, Notification.Name is just a string. You can create the notification name like this:

        let notification = NSNotification.Name(rawValue: PKPassLibraryNotificationName.PKPassLibraryDidChange.rawValue)
        NotificationCenter.default.addObserver(forName: notification, object: myPassLibrary, queue: nil) { (notification) in
            // do something here
        }
    

    This compiles, but I've never seen it do anything useful. PKPassLibrary just never seems to post notifications.