【发布时间】:2021-09-13 09:31:59
【问题描述】:
我正在使用 SwiftUI 开发这个 iOS 应用。
如何检测用户何时单击通知权限警报的允许按钮?它应该只发生一次,我需要在用户第一次点击按钮时将其带到特定的选项卡。这可能吗?
【问题讨论】:
标签: swift push-notification notifications popup alert
我正在使用 SwiftUI 开发这个 iOS 应用。
如何检测用户何时单击通知权限警报的允许按钮?它应该只发生一次,我需要在用户第一次点击按钮时将其带到特定的选项卡。这可能吗?
【问题讨论】:
标签: swift push-notification notifications popup alert
您可以添加属性onclick="location.href='https://google.com';"。
但是你能发布 coden-ps 吗?你使用javascript和php吗?
【讨论】:
我找到了解决方案。为了创建这种行为,我只是在 UserDefault 中保存了一个 Bool 值。
func registerForPushNotifications() {
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, _ in
print("Permission granted: \(granted)")
self?.userDefault.set(granted, forKey: "notificationGranted")
guard granted else {
return
}
self?.getNotificationSettings()
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else {
return
}
// Saving the value to permit to take the user to the Notification screen only the first time
if (self.userDefault.value(forKey: "permissionPopUpIsShowed") == nil) {
self.userDefault.set(true, forKey: "permissionPopUpIsShowed")
}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
在 onAppear 方法内的 Content View 中,您可以这样做:
if appDelegate.userDefault.bool(forKey: "permissionPopUpIsShowed") == true && appDelegate.userDefault.bool(forKey: "notificationGranted") == true {
tabSelection = .notifications
appDelegate.userDefault.set(false, forKey: "permissionPopUpIsShowed")
}
通过这种方式,您将能够选择一个新视图(通过 tabSelection)并仅获得一次此行为(当显示通知权限弹出窗口时)。
【讨论】: