iOS 8等待本地通知权限

iOS 8 wait for local notification permission

我想在我的应用首次启动时安排 UILocalNotification。在 iOS 8 这需要我通过此调用请求用户许可:

[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

这会弹出一个对话框,询问用户的权限。

有没有办法在安排我的通知之前等待用户回答?我宁愿不等待任何其他用户操作,例如应用程序中的按钮按下或视图控制器更改。

在 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.