iosswiftswift3uilabelunnotificationrequest

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


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.


Solution

  • 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 :)