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

Perform segue after notification access has been granted

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

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

}