gpt4 book ai didi

swift - 授予通知访问权限后执行 segue

转载 作者:行者123 更新时间:2023-11-30 10:29:40 25 4
gpt4 key购买 nike

我想知道在从对话框授予远程通知访问权限后如何执行模式转场。我已在应用程序委托(delegate)中设置了远程通知。

func registerANSForApplication(_ application: UIApplication,withBlock block: @escaping (_ granted:Bool) -> (Void)){
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instange ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
AppDelegate.isToken = result.token
}
}
let current = UNUserNotificationCenter.current()
let options : UNAuthorizationOptions = [.sound, .badge, .alert]

current.requestAuthorization(options: options) { (granted, error) in
guard granted else{
return
}
if error != nil{
print(error?.localizedDescription ?? "")
}else{
Messaging.messaging().delegate = self
current.delegate = self
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}

}

然后,在我的 View Controller 中,我有以下代码:

let appDelegate = UIApplication.shared.delegate as! 
appDelegate.registerANSForApplication(UIApplication.shared) { (granted) -> (Void) in
self.performSegue(withIdentifier: "MembershipVC", sender: nil)
}

问题在于,无论用户允许还是拒绝访问通知,segue 都不会执行。

感谢您的帮助。

最佳答案

您必须调用block参数

替换

current.requestAuthorization(options: options) { (granted, error) in
guard granted else{
return
}
if error != nil{
print(error?.localizedDescription ?? "")
}else{
Messaging.messaging().delegate = self
current.delegate = self
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}

current.requestAuthorization(options: options) { (granted, error) in
if error != nil {
print(error?.localizedDescription ?? "")
block(false)
} else {
Messaging.messaging().delegate = self
current.delegate = self
DispatchQueue.main.async {
application.registerForRemoteNotifications()
block(granted)
}
}

}

关于swift - 授予通知访问权限后执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59409202/

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