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



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

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()
}
}
}
}

然后,在我的视图控制器中,我有以下代码:

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)
}
}
}

最新更新