Update UILabel text value , when changing the user permission for notification in application setting page?

一世执手 提交于 2019-12-11 06:23:46

问题


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

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

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

func checkNotificationPermission()

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


回答1:


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

I have created global object

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

and one method to check it

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

Hope it is helpful to you :)



来源:https://stackoverflow.com/questions/50645149/update-uilabel-text-value-when-changing-the-user-permission-for-notification-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!