Detect when asked and granted permission to iOS no

2019-08-10 05:13发布

In the iOS client when you first run it you get a UIAlert asking for permission to grant access to send this person notifications.

I'm wondering if its possible to first:

  1. Catch when this alert will fire (before it happens)
  2. Catch what response is given (cancel or ok) (after it happens)

Is there such a way to get callbacks for the above two scenarios?

Many thanks

2条回答
祖国的老花朵
2楼-- · 2019-08-10 05:40

On iOS 8 and later, implement the following method in your AppDelegate:

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

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

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

This method is a UIApplicationDelegate method, so all you need to do is implement it in your MyAppDelegate.m file.

From the 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.

查看更多
神经病院院长
3楼-- · 2019-08-10 05:40

The application itself is causing the alert to pop, so you don't need to "catch" anything.

Once you call - (void)registerForRemoteNotifications, the OS will fire the alert.

Read here for more details.

查看更多
登录 后发表回答