如果被拒绝,如何询问通知权限?

Hen*_*bat 4 push-notification ios swift

我想在我的家庭控制器中第二次获得许可,例如,我可以通过编程方式进行吗?

我的意思是我的用户在第一次禁用它,我不想让他另一个选项来获取通知.

Ras*_*n L 8

你不允许这样做.通知弹出窗口将在用户第一次打开应用程序时提示.你可以做的是检查用户是否允许这样做.然后你可以打开设置页面(这基本上就是你在这种情况下可以做的):

let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if !isRegisteredForRemoteNotifications {
    UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)


小智 8

这实际上是不可能的。您只有一次机会来提示他们授予权限。这就是为什么大多数应用程序会显示自定义视图来解释为什么需要某种权限。如果用户单击“是”,则他们会启动实际的权限警报。如果他们已经拒绝了权限,您需要检查应用程序是否具有某些权限并提示他们进入设置以激活所需的内容。

以下是如何检查他们是否已授予许可的示例


Jul*_*ius 6

在用户选择允许之后,您不能要求许可.您可以做的是检查是否允许权限并将用户重定向到应用程序的设置.

如何检查权限的授权状态取决于您要授权的服务类型.您可以使用以下代码将用户重定向到设置:

迅速

UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

目标C.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
Run Code Online (Sandbox Code Playgroud)


Mil*_*sáľ 5

用户启用或拒绝通知后,您无法显示标准弹出窗口。在这种情况下,显示一个alertController来通知用户这种情况并为她提供一个导航到设置的按钮是很常见的:

let alert = UIAlertController(title: "Unable to use notifications",
                              message: "To enable notifications, go to Settings and enable notifications for this app.",
                              preferredStyle: UIAlertControllerStyle.alert)

let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(okAction)

let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { _ in
    // Take the user to Settings app to possibly change permission.
    guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else { return }
    if UIApplication.shared.canOpenURL(settingsUrl) {
        UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
            // Finished opening URL
        })
    }
})
alert.addAction(settingsAction)

self.present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

该代码的灵感来自苹果工程师的一个类似的相机访问示例。