确定是否曾在 Swift 中调用过 `registerUserNotificationSettings`

Determining whether a call to `registerUserNotificationSettings` has ever been made in Swift

我想确定用户是否收到过请求 registerUserNotificationSettings UIUserNotificationSettings 的警报。

例如,在访问照片时,可以确定是否已请求访问照片并授予权限 PHPhotoLibrary.authorizationStatus()。它returns的状态为.Authorized.Denied.Restricted.NotDetermined。如果迄今为止没有请求,则返回 .NotDetermined

同样,当第一次注册通知并获得用户的许可时 UIApplication.sharedApplication().registerUserNotificationSettings,可以确定注册的通知类型,.Badge.Alert , .Sound, .None。 (注意:.None 可能表示没有请求 有请求,但用户选择不通知。)

Question:

But is there a way to determine whether the registerUserNotificationSettings has ever been called before? i.e. is there an equivalent to Photos .NotDetermined?

当您调用 registerUserNotificationSettings 时,UIApplicationDelegate 方法:

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings)

将会打电话通知您结果。使用此方法可以保存相关的状态信息,以便下次应用程序运行时,您可以知道之前是否调用过。