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

Aus*_*nyi 0 permissions notifications objective-c apple-push-notifications ios

在我的登录中,我UIPageViewController在末尾包含一个 \xe2\x80\x98primer\xe2\x80\x99 屏幕,用于授权通知。用户将点击标有 \xe2\x80\x9cEnable Notifications\xe2\x80\x9d 的按钮,然后将出现通知权限对话框。我该如何实现这个目标?

\n

Mat*_*usz 5

你可以输入:

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.
Run Code Online (Sandbox Code Playgroud)

迅速

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.
Run Code Online (Sandbox Code Playgroud)

在你的IBAction.

请记住在您拥有的文件中import UserNotifications添加SwiftObjective-C#import <UserNotifications/UserNotifications.h>,并确保在- -下激活。IBActionPush NotificationtargetCapabilitiesPush notification