对用户权限授予结果执行功能?

时间:2017-11-17 10:19:30

标签: ios swift permissions closures

我询问用户是否允许通知,然后我想在他们批准的情况下设置动画。我不确定如何实现这一点,因为权限是异步的,我不能立即检测到结果执行功能?

这是我目前的代码:

用户点击按钮并触发调用以授予权限,并使用闭包在完成时勾选按钮

func userDidTapNotifications() {
    self.appDelegate.setupNotifications { (success) -> Void in
        if success {
            notificationsGranted()
        }
    }
}

函数显示批准弹出并完成关闭,问题是它将在用户选择async之前完成,因此我在用户授予访问权限之前完成true,从而导致问题。

func setupNotifications(completion: (_ success: Bool) -> Void) {
    center.requestAuthorization(options: [.badge, .alert , .sound]) { (granted, error) in
        if granted {
            DispatchQueue.main.async(execute: {
                UIApplication.shared.registerForRemoteNotifications()
            })
        }
    }
    completion(true)
}

在此之后,我在关闭完成后调用我的最终函数:

func notificationsGranted() {
    let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
    if isRegisteredForRemoteNotifications {
        self.permissionsView.notificationsSwitch.setSelected(true, animated: true)
        self.userDefaults.set(true, forKey: "notificationsApproved")
        arePermissionsGranted()
    }
}

提供权限提醒的正确方法是什么,然后根据响应采取行动?

1 个答案:

答案 0 :(得分:1)

将完成处理程序存储在AppDelegate中的全局对象中,并且当调用didRegisterRemoteNotification方法时,只需调用已存储的完成处理程序... 在您的ViewController中,您应该调用setUpNotification方法。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var notificationCompletion: ((Bool)->Void)?



    func setUpNotification(completion: @escaping (Bool)->Void) {
        self.notificationCompletion = completion //Store the completion in a global property to use later.

        //Invoke Your Notification Registration methods...
        //Do not invoke the completion handle here...
    }
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //Permission is granted. Now invoke the completion that you have store to use later.
        if let completion = self.notificationCompletion {
            completion(true)
        }
    }
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        //Permission is not granted/failed. Now invoke the completion that you have store to use later.
        if let completion = self.notificationCompletion {
            completion(false)
        }
    }
}


ViewController.swift

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.setUpNotification(completion: { isPermissionGranted in
                if isPermissionGranted {
                    DispatchQueue.main.async {
                        self.notificationPermissionIsGranted()
                    }
                }

            })
        }
    }
    func notificationPermissionIsGranted() {
        //Animate Your View..
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}