如何订阅 PKPassLibraryNotificationName

How to subscribe to PKPassLibraryNotificationName

我需要观察 https://developer.apple.com/documentation/passkit/pkpasslibrarynotificationname

中指定的 .PKPassLibraryDidChange 和 .PKPassLibraryRemotePaymentPassesDidChange 通知

但是它们不是 NSNotification.Name 的子类,所以

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

无法编译。

是否需要任何额外的导入才能在 NotificationCenter 上观察 PKPassLibraryNotificationName

要回答您的实际问题,Notification.Name 只是一个字符串。您可以像这样创建通知名称:

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

这可以编译,但我从未见过它有什么用处。 PKPassLibrary 似乎从来没有 post 通知。

我可以确认通知工作正常。

根据 Apple 的说法:

PassKit posts this notification on an arbitary queue, and only does so if an instance of PKPassLibrary exists

因此您需要初始化 PKPassLibrary() 并保存到一些 属性 然后您将开始接收通知。