【发布时间】:2018-07-25 13:54:29
【问题描述】:
例如,我想在我的家庭控制器中第二次请求许可,我可以通过编程方式进行吗?
我的意思是我的用户第一次禁用它,我不想让他有另一个选项来获得通知。
【问题讨论】:
标签: ios swift push-notification
例如,我想在我的家庭控制器中第二次请求许可,我可以通过编程方式进行吗?
我的意思是我的用户第一次禁用它,我不想让他有另一个选项来获得通知。
【问题讨论】:
标签: ios swift push-notification
你不能这样做。通知弹窗会在用户第一次打开应用程序时进行提示。您可以做的是检查用户是否不允许这样做。然后你可以打开设置页面(这基本上是你可以在这种情况下做的):
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if !isRegisteredForRemoteNotifications {
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
}
Swift 5.x
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if !isRegisteredForRemoteNotifications {
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
}
【讨论】:
这实际上是不可能的。您只能获得一次提示他们授予权限的机会。这就是为什么大多数应用程序会显示一个自定义视图来解释为什么需要特定权限。如果用户点击“是”,他们就会启动实际的权限警报。 如果他们已经拒绝了权限,您需要检查应用是否具有某些权限并提示他们进入设置以激活所需的权限。
这里有一个example,告诉您如何检查他们是否已授予权限。
【讨论】:
在用户选择允许与否之后,您不能请求权限。您可以做的是检查权限是否不允许并将用户重定向到应用程序的设置。
您如何检查权限的授权状态取决于您要授权的服务类型。您可以使用以下代码将用户重定向到设置:
斯威夫特
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
目标 C
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
【讨论】:
在用户启用或拒绝通知后,您无法显示标准弹出窗口。在这种情况下,显示一个 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)
代码的灵感来自similar example,供 Apple 工程师访问相机。
【讨论】:
在这种情况下,您可以显示将用户导航到设置的警报
let alert = UIAlertController(title: "Allow notification Access", message: "Allow notification access in your device settings.", preferredStyle: UIAlertController.Style.alert)
// Button to Open Settings
alert.addAction(UIAlertAction(title: "Settings", style: UIAlertAction.Style.default, handler: { action in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)")
})
}
}))
alert.addAction(UIAlertAction(title: "Close", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
【讨论】: