Hen*_*bat 4 push-notification ios swift
我想在我的家庭控制器中第二次获得许可,例如,我可以通过编程方式进行吗?
我的意思是我的用户在第一次禁用它,我不想让他另一个选项来获取通知.
你不允许这样做.通知弹出窗口将在用户第一次打开应用程序时提示.你可以做的是检查用户是否允许这样做.然后你可以打开设置页面(这基本上就是你在这种情况下可以做的):
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if !isRegisteredForRemoteNotifications {
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)
在用户选择允许之后,您不能要求许可.您可以做的是检查是否允许权限并将用户重定向到应用程序的设置.
如何检查权限的授权状态取决于您要授权的服务类型.您可以使用以下代码将用户重定向到设置:
迅速
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)
用户启用或拒绝通知后,您无法显示标准弹出窗口。在这种情况下,显示一个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)
该代码的灵感来自苹果工程师的一个类似的相机访问示例。
| 归档时间: |
|
| 查看次数: |
2634 次 |
| 最近记录: |