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

杰克德姆

我问用户他们是否允许通知,如果他们同意,我想动画一个勾号。我不确定如何实现这一点,因为权限是异步的,我无法立即检测到结果来执行功能?

这是我当前的代码:

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

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

函数显示批准弹出窗口并完成关闭,问题是它将在用户选择一个选项作为异步之前完成,所以我在用户授予访问权限之前完成 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:

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

呈现权限警报的正确方法是什么,然后根据响应采取行动?

赛义德·卡马尔·阿巴斯

将您的完成处理程序存储在您的全局对象中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.
    }


}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

授予用户更改功能的权限

来自分类Dev

如何授予尚未定义的功能的执行权限

来自分类Dev

授予用户的权限不足以执行此操作(rsAccessDenied)

来自分类Dev

如何在 Ubuntu 中授予 Root 用户文件的执行权限?

来自分类Dev

权限要求授予执行权限

来自分类Dev

授予apache用户权限

来自分类Dev

授予目录执行权限,但不授予文件执行权限

来自分类Dev

授予单个用户写权限

来自分类Dev

授予单个用户写权限

来自分类Dev

如何授予用户root权限?

来自分类Dev

经典ASP-哪些用户需要为存储过程授予的执行权限

来自分类Dev

在目录树中授予“其他用户”的读取/执行权限

来自分类Dev

授予用户仅执行一个存储过程但不能运行其他查询的权限

来自分类Dev

向用户授予本地管理员权限以执行 VBS 脚本

来自分类Dev

执行Karma授予权限被拒绝错误

来自分类Dev

如何通过umask授予执行权限

来自分类Dev

授予cron执行命令的权限

来自分类Dev

哪种软件负责检查文件的权限以授予用户访问权限或不授予用户访问权限?

来自分类Dev

哪种软件负责检查文件的权限以授予用户访问权限或不授予用户访问权限?

来自分类Dev

区分每个apache用户并授予权限

来自分类Dev

android shell已被授予超级用户权限

来自分类Dev

授予用户访问分区上目录的权限

来自分类Dev

授予用户访问passwd命令的权限

来自分类Dev

如何为每个用户授予不同的权限?

来自分类Dev

授予用户访问分区上目录的权限

来自分类Dev

授予用户访问passwd命令的权限

来自分类Dev

授予用户无密码或sudo的rm权限

来自分类Dev

向表授予用户读写权限

来自分类Dev

向TFS用户授予ManageBuildResources权限

Related 相关文章

热门标签

归档