在被询问并授予 iOS 通知权限时检测?

Detect when asked and granted permission to iOS notifications?

在 iOS 客户端中,当您第一次 运行 它时,您会收到一个 UIAlert,请求授予访问权限以向此人发送通知。

我想知道是否可以先:

  1. 捕捉此警报何时触发(在它发生之前)
  2. 捕捉给出的响应(取消或确定)(发生后)

以上两种场景有没有这样的回调方法?

非常感谢

在 iOS 8 及更高版本上,在您的 AppDelegate 中实现以下方法:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

    // You could check here [[UIApplication sharedApplication] currentUserNotificationSettings]

    // This is where you set up your local notification
}

这个方法是一个UIApplicationDelegate方法,所以你需要做的就是在你的MyAppDelegate.m文件中实现它。

来自documentation

Tells the delegate what types of notifications may be used to get the user’s attention.

Parameters

application: The app object that registered the user notification settings.

notificationSettings: The user notification settings that are available to your app.

The settings in this object may be different than the ones you originally requested.

Discussion

Apps that use local or remote notifications to alert the user to new information must register the types of notifications they want to use by calling the registerUserNotificationSettings: method of the app object. (In apps that link on versions of iOS prior to 8.0, registration can also happen implicitly when you schedule a local notification.) Your app’s request is combined with the user’s current preferences to determine what notification types are allowed and the results are delivered to this method in the notificationSettings parameter.

The first time you register your app’s preferred notification types, the system asks the user whether your app should be allowed to deliver notifications and stores the user’s response. The system does not prompt the user during subsequent registration attempts. The user can always change the notification preferences using the Settings app.

Because the user’s preferences can change, you should always check the contents of the notificationSettings parameter. These settings control only whether the user is notified about a local or remote notification. The notification is still delivered to your app at the appropriate times.

应用程序本身会导致弹出警报,因此您不需要"catch"任何操作。

一旦您调用 - (void)registerForRemoteNotifications,OS 将触发警报。

阅读 here 了解更多详情。