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

标签 swift appdelegate remote-notifications

我想知道在从对话框授予远程通知访问权限后如何执行模式转场。我已在应用程序委托(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/

相关文章:

swift - 如何在 Swift 5.0 中通过 CAShapeLayer 以点开始和结束一行

swift - 如何从另一个场景调用函数?

ios - 从 AppDelegate 呈现 ViewController

ios - 如何将 Iphone 的远程通知传递给 Watch OS?

swift - mixLatest 在 Just 与 Future 中具有不同的行为

swift - 我该如何处理这个初始值设定项?

iOS 13 : Swift - 'Set application root view controller programmatically' does not work

ios - 从 Testflight 打开按钮启动应用程序时崩溃

ios - iOS中的远程通知和静默通知有什么区别?

ios - APNS生产环境和开发环境didReceiveRemoteNotification行为差异