更新 UILabel 文本值,在应用程序设置页面中更改通知的用户权限时? [英] Update UILabel text value , when changing the user permission for notification in application setting page?

查看:28
本文介绍了更新 UILabel 文本值,在应用程序设置页面中更改通知的用户权限时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的场景中,用户将收到应用程序中接收通知的警报.如果用户单击不允许",则 UILabel 将更新为未启用".如果用户想要更改通知,用户将被导航到应用程序设置页面以更改通知权限状态.

In my scenario, User will get an alert for receiving Notification in application. If the user clicks on "Don't Allow" UILabel is updated with "Not enabled". If the user wants to change the notification,User will be navigated to application setting page to change the notification permission status.

func checkNotificationPermission(){

UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){


        (granted, error) in

        if granted == true {

            DispatchQueue.main.async {
                    print("notificaation access true")
                     self.notificationAccessLbl?.text = "Enabled"         
        }
        }
        else {
            DispatchQueue.main.async {
                self.notificationAccessLbl?.text = "Not enabled" 
            }
        } 
    }
    UIApplication.shared.registerForRemoteNotifications() }

但是当用户返回应用程序时,当用户从设置页面进入应用程序时,UILabel 不会更新.

But when the user comes back to application, The UILabel is not getting updated when the user comes to application from Setting page.

用于在用户从设置页面到应用程序后更新应用程序中的 UILabel.我打过电话

for Updating the UILabel in application after the user comes from setting page to application. I Have Called

func checkNotificationPermission()

更新 ViewDidAppear() 方法中的 UILabel 值,我在 applicationwillbecomeactive method() 中注册了一个函数,请帮助我.

to update UILabel Value in ViewDidAppear() Method and I register the a function in applicationwillbecomeactive method() Kindly help me in this.

推荐答案

我在应用程序的设置页面中切换,允许用户启用禁用推送,这将在服务器上发送,但在该用户之前必须允许从设置页面推送设备.这是我的解决方案

I have switch in setting page in application which allows user to enable disable push and that will be send on server but before that user must have allowed push from settings page of Device. here is my solution

我已经创建了全局对象

var isPushEnabledFromSettings = false {
    didSet {
         // you can set label value here in main queue
    }
}

和一种检查方法

func isPushPermissionGiven (permission:@escaping (Bool) -> ()) {
    if #available(iOS 10.0, *) {
        let current = UNUserNotificationCenter.current()
        current.getNotificationSettings(completionHandler: {settings in
            switch settings.authorizationStatus {
            case .notDetermined:
                permission(false)
            case .denied:
                permission(false)
            case .authorized:
                permission(true)
            }
        })
    } else {
        // Fallback on earlier versions
        if UIApplication.shared.isRegisteredForRemoteNotifications {
            permission(true)


        } else {
            permission(false)

        }
    }
}

并且在视图中确实加载了这些行

and in view did load added these lines

    self.isPushPermissionGiven { (permission) in
        self.isPushEnabledFromSettings = permission
    }

    NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationDidBecomeActive, object: nil, queue: .main) {[weak self] (notificaiont) in
        guard let strongSelf = self  else {return }

        strongSelf.isPushPermissionGiven { (permission) in
            DispatchQueue.main.async {
                strongSelf.isPushEnabledFromSettings = permission
            }
        }
    }

现在我在设置页面中切换,允许用户启用禁用推送

Now I have switch in setting page which allows user to enable disable push

@objc func switchChanged (sender:UISwitch) {

    guard self.isPushEnabledFromSettings else {
        AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "Please grant push permission from settings")
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
            sender.setOn(false, animated: false)
        })
        return

    }
}



  func navigateUesrToSettings (withTitle title:String = "YourApp", withMessage message:String) {
        let alertController = UIAlertController (title: title, message: message, preferredStyle: .alert)

        let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
            guard let _ = URL(string: UIApplicationOpenSettingsURLString) else {
                return
            }

            self.navigate(To: UIApplicationOpenSettingsURLString)

        }
        alertController.addAction(settingsAction)
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        alertController.addAction(cancelAction)

        AppDelegate.sharedDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
    }

希望对你有帮助:)

这篇关于更新 UILabel 文本值,在应用程序设置页面中更改通知的用户权限时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆