gpt4 book ai didi

iOS 8 等待本地通知权限

转载 作者:行者123 更新时间:2023-11-28 21:50:15 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

关于iOS 8 等待本地通知权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com