gpt4 book ai didi

ios - 尝试使用 localnotification 呈现其 View 不在窗口层次结构中的 UIAlertController

转载 作者:行者123 更新时间:2023-11-28 09:43:15 25 4
gpt4 key购买 nike

<分区>

我试图在用户点击本地通知后显示 AlertView。 AlertView 有取消或确定的选项。

extension ViewController:UNUserNotificationCenterDelegate{


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

print("Tapped in notification")
if(defaults.object(forKey: "alertOn") != nil){


// Create the alert controller
let alertController = UIAlertController(title: "Some text", message: "Some text again", preferredStyle: .alert)


// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in

}

let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}

// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)

// Present the controller
self.present(alertController, animated: true, completion: nil)

}
}


func triggerNotification(){

print("notification will be triggered in five seconds..Hold on tight")
let content = UNMutableNotificationContent()
content.title = "SomeText"
content.body = "Some more text"
content.sound = UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)

UNUserNotificationCenter.current().delegate = self as! UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().add(request){(error) in

if (error != nil){

print(error?.localizedDescription)
}
}
}

它应该向我显示带有确定或取消请求选项的警报 View 。相反,它向我显示消息 UIAlertController whose view is not in the window hierarchy

当我将 alertview 放在 viewdidapear 中时,它工作正常,但是当我将它放在 userNotificationCenter 中时,我的 AlertView 没有附加到主视图。

代码简介

extension ViewController:UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

Present UIAlertView

}
}

ViewController{

Call to notification when app is in background
triggerNotification()

triggerNotification(){
Definition
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com