ios - 如何在 IBAction 中请求通知权限?

标签 ios objective-c permissions notifications apple-push-notifications

在我的入门中,我有一个UIPageViewController,在末尾包含一个用于授权通知的“入门”屏幕。用户点击标有“启用通知”的按钮,就会出现通知权限对话框。我该如何实现?

最佳答案

您可以输入:

objective-C

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
                      completionHandler:^(BOOL granted, NSError * _Nullable error) {
                          // Enable or disable features based on authorization.
                      }];
[[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification.

swift

let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        // Enable or disable features based on authorization.
    }
    UIApplication.shared.registerForRemoteNotifications() // you can also set here for local notification.

在你的 IBAction 里面.

请记住同时添加 import UserNotifications对于 Swift#import <UserNotifications/UserNotifications.h>对于 Objective-C 在您有 IBAction 的文件中并确保 Push Notificationtarget 下激活- Capabilities - Push notification .

关于ios - 如何在 IBAction 中请求通知权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46757966/

相关文章:

iOS - 更改子类 UIView 的 subview 的框架(使用启用自动布局的 Storyboard)

ios - 通过点击 UIView 外部来关闭 ViewController 内的 UIView

objective-c - NSLocalizedString 不起作用

php - WordPress 只能使用 777 权限

powershell - 如何使用 PowerShell 取得文件所有权?

ios - PBXCp 错误 ShareKit.bundle 未构建

ios - #define 改变多参数方法名

objective-c - 初始化枚举值时出错

objective-c - 在屏幕上移动 UIView,为什么会这样?

android - 执行命令时出错 : permission denied