Swift 4 - Refresh View after updating privacy settings CLLocationManager









up vote
1
down vote

favorite












I am creating a location based app, and when the user has denied location access an alert view pops up asking the user go to the settings page and update the settings to allow location access. If the user allows access and then presses back the view does not refresh with the updated settings allowing the update of location.



Is there a function that can be run after the user comes back into the app from the settings page that will refresh and start updating location.



I appreciate any advice or help. Thank you.



USER SEES ALERT:
alert image



AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:
enter image description here



func alertForNoLocationAccess()

let alert = UIAlertController(title: "Allow Location Access", message: "Cannot add location data and map coordinates to your entries without access. Press settings to update or cancel to deny access.", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: action in

if let url = URL(string:UIApplication.openSettingsURLString)
if UIApplication.shared.canOpenURL(url)
if #available(iOS 10.0, *)
UIApplication.shared.open(url, options: [:], completionHandler: nil)
else
UIApplication.shared.openURL(url)




))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

self.present(alert, animated: true)










share|improve this question

























    up vote
    1
    down vote

    favorite












    I am creating a location based app, and when the user has denied location access an alert view pops up asking the user go to the settings page and update the settings to allow location access. If the user allows access and then presses back the view does not refresh with the updated settings allowing the update of location.



    Is there a function that can be run after the user comes back into the app from the settings page that will refresh and start updating location.



    I appreciate any advice or help. Thank you.



    USER SEES ALERT:
    alert image



    AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:
    enter image description here



    func alertForNoLocationAccess()

    let alert = UIAlertController(title: "Allow Location Access", message: "Cannot add location data and map coordinates to your entries without access. Press settings to update or cancel to deny access.", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: action in

    if let url = URL(string:UIApplication.openSettingsURLString)
    if UIApplication.shared.canOpenURL(url)
    if #available(iOS 10.0, *)
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
    else
    UIApplication.shared.openURL(url)




    ))

    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert, animated: true)










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am creating a location based app, and when the user has denied location access an alert view pops up asking the user go to the settings page and update the settings to allow location access. If the user allows access and then presses back the view does not refresh with the updated settings allowing the update of location.



      Is there a function that can be run after the user comes back into the app from the settings page that will refresh and start updating location.



      I appreciate any advice or help. Thank you.



      USER SEES ALERT:
      alert image



      AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:
      enter image description here



      func alertForNoLocationAccess()

      let alert = UIAlertController(title: "Allow Location Access", message: "Cannot add location data and map coordinates to your entries without access. Press settings to update or cancel to deny access.", preferredStyle: .alert)

      alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: action in

      if let url = URL(string:UIApplication.openSettingsURLString)
      if UIApplication.shared.canOpenURL(url)
      if #available(iOS 10.0, *)
      UIApplication.shared.open(url, options: [:], completionHandler: nil)
      else
      UIApplication.shared.openURL(url)




      ))

      alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

      self.present(alert, animated: true)










      share|improve this question













      I am creating a location based app, and when the user has denied location access an alert view pops up asking the user go to the settings page and update the settings to allow location access. If the user allows access and then presses back the view does not refresh with the updated settings allowing the update of location.



      Is there a function that can be run after the user comes back into the app from the settings page that will refresh and start updating location.



      I appreciate any advice or help. Thank you.



      USER SEES ALERT:
      alert image



      AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:
      enter image description here



      func alertForNoLocationAccess()

      let alert = UIAlertController(title: "Allow Location Access", message: "Cannot add location data and map coordinates to your entries without access. Press settings to update or cancel to deny access.", preferredStyle: .alert)

      alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: action in

      if let url = URL(string:UIApplication.openSettingsURLString)
      if UIApplication.shared.canOpenURL(url)
      if #available(iOS 10.0, *)
      UIApplication.shared.open(url, options: [:], completionHandler: nil)
      else
      UIApplication.shared.openURL(url)




      ))

      alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

      self.present(alert, animated: true)







      ios swift cllocationmanager






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 16:27









      Jason P.

      23617




      23617






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Register ViewController/AppDelegate to NotificationCenter to notify when user has opened the app from Settings.



          NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)


          On user opening the app, request for location updates again to CLLocationManager. If user has accepted the request already then CLLocationManager will start updating user's location without interrupting.



          @objc func willEnterForeground() 

          //Register for
          registerForLocationUpdates()


          func registerForLocationUpdates()

          self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
          self.locationManager.requestWhenInUseAuthorization()

          if CLLocationManager.locationServicesEnabled()

          locationManager.delegate = self
          locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
          locationManager.startUpdatingLocation()







          share|improve this answer




















          • This is the correct way to fix it. I have implemented it and it works perfectly. Added the notification listener to the viewDidLoad and the function you listed. Thank you
            – Jason P.
            Nov 10 at 21:00


















          up vote
          3
          down vote













          Check Apple's documentation, and you find this:




          "When you request authorization, or when your app's authorization
          status changes, use the locationManager(_:didChangeAuthorization:)
          method of your delegate object to process the changes. Listing 3 shows
          an implementation of that method that enables or disables features
          based on the app's current authorization level."




          Obviously this delegate method will also be triggered if the user decides to turn off your permission to get location data.



          If you are showing this alert, you should also use this notification to dismiss the alert, in case the user went to Settings manually without using the button in your alert.






          share|improve this answer




















          • Thank you for the comment, I have implemented the delegate methods already, the issue is that when the app enters the foreground and the user re-enters my viewController from the settings view no action is taken. The CLLocation delegate methods do not handle that.
            – Jason P.
            Nov 10 at 20:59


















          up vote
          0
          down vote













          Implement Delegate method of CLLocationManagerDelegate



          // MARK: - CLLocationManagerDelegate
          extension LocationTracker: CLLocationManagerDelegate

          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
          print(locations)
          guard let location = locations.first else return
          lastLocation = location
          print(LocationTracker.shared.lastLocation)
          print("location = (location.coordinate.latitude) (location.coordinate.longitude)")
          locateMeCallback?(location)


          func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
          print(error.localizedDescription)


          func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
          enableLocationServices()





          /// enableLocationService goes like this



          func enableMyAlwaysFeatures() 
          locationManager.allowsBackgroundLocationUpdates = true
          locationManager.pausesLocationUpdatesAutomatically = true
          locationManager.startUpdatingLocation()
          locationManager.delegate = self






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240978%2fswift-4-refresh-view-after-updating-privacy-settings-cllocationmanager%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Register ViewController/AppDelegate to NotificationCenter to notify when user has opened the app from Settings.



            NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)


            On user opening the app, request for location updates again to CLLocationManager. If user has accepted the request already then CLLocationManager will start updating user's location without interrupting.



            @objc func willEnterForeground() 

            //Register for
            registerForLocationUpdates()


            func registerForLocationUpdates()

            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.requestWhenInUseAuthorization()

            if CLLocationManager.locationServicesEnabled()

            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()







            share|improve this answer




















            • This is the correct way to fix it. I have implemented it and it works perfectly. Added the notification listener to the viewDidLoad and the function you listed. Thank you
              – Jason P.
              Nov 10 at 21:00















            up vote
            2
            down vote



            accepted










            Register ViewController/AppDelegate to NotificationCenter to notify when user has opened the app from Settings.



            NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)


            On user opening the app, request for location updates again to CLLocationManager. If user has accepted the request already then CLLocationManager will start updating user's location without interrupting.



            @objc func willEnterForeground() 

            //Register for
            registerForLocationUpdates()


            func registerForLocationUpdates()

            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.requestWhenInUseAuthorization()

            if CLLocationManager.locationServicesEnabled()

            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()







            share|improve this answer




















            • This is the correct way to fix it. I have implemented it and it works perfectly. Added the notification listener to the viewDidLoad and the function you listed. Thank you
              – Jason P.
              Nov 10 at 21:00













            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Register ViewController/AppDelegate to NotificationCenter to notify when user has opened the app from Settings.



            NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)


            On user opening the app, request for location updates again to CLLocationManager. If user has accepted the request already then CLLocationManager will start updating user's location without interrupting.



            @objc func willEnterForeground() 

            //Register for
            registerForLocationUpdates()


            func registerForLocationUpdates()

            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.requestWhenInUseAuthorization()

            if CLLocationManager.locationServicesEnabled()

            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()







            share|improve this answer












            Register ViewController/AppDelegate to NotificationCenter to notify when user has opened the app from Settings.



            NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)


            On user opening the app, request for location updates again to CLLocationManager. If user has accepted the request already then CLLocationManager will start updating user's location without interrupting.



            @objc func willEnterForeground() 

            //Register for
            registerForLocationUpdates()


            func registerForLocationUpdates()

            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.requestWhenInUseAuthorization()

            if CLLocationManager.locationServicesEnabled()

            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 16:38









            Sateesh

            1,664715




            1,664715











            • This is the correct way to fix it. I have implemented it and it works perfectly. Added the notification listener to the viewDidLoad and the function you listed. Thank you
              – Jason P.
              Nov 10 at 21:00

















            • This is the correct way to fix it. I have implemented it and it works perfectly. Added the notification listener to the viewDidLoad and the function you listed. Thank you
              – Jason P.
              Nov 10 at 21:00
















            This is the correct way to fix it. I have implemented it and it works perfectly. Added the notification listener to the viewDidLoad and the function you listed. Thank you
            – Jason P.
            Nov 10 at 21:00





            This is the correct way to fix it. I have implemented it and it works perfectly. Added the notification listener to the viewDidLoad and the function you listed. Thank you
            – Jason P.
            Nov 10 at 21:00













            up vote
            3
            down vote













            Check Apple's documentation, and you find this:




            "When you request authorization, or when your app's authorization
            status changes, use the locationManager(_:didChangeAuthorization:)
            method of your delegate object to process the changes. Listing 3 shows
            an implementation of that method that enables or disables features
            based on the app's current authorization level."




            Obviously this delegate method will also be triggered if the user decides to turn off your permission to get location data.



            If you are showing this alert, you should also use this notification to dismiss the alert, in case the user went to Settings manually without using the button in your alert.






            share|improve this answer




















            • Thank you for the comment, I have implemented the delegate methods already, the issue is that when the app enters the foreground and the user re-enters my viewController from the settings view no action is taken. The CLLocation delegate methods do not handle that.
              – Jason P.
              Nov 10 at 20:59















            up vote
            3
            down vote













            Check Apple's documentation, and you find this:




            "When you request authorization, or when your app's authorization
            status changes, use the locationManager(_:didChangeAuthorization:)
            method of your delegate object to process the changes. Listing 3 shows
            an implementation of that method that enables or disables features
            based on the app's current authorization level."




            Obviously this delegate method will also be triggered if the user decides to turn off your permission to get location data.



            If you are showing this alert, you should also use this notification to dismiss the alert, in case the user went to Settings manually without using the button in your alert.






            share|improve this answer




















            • Thank you for the comment, I have implemented the delegate methods already, the issue is that when the app enters the foreground and the user re-enters my viewController from the settings view no action is taken. The CLLocation delegate methods do not handle that.
              – Jason P.
              Nov 10 at 20:59













            up vote
            3
            down vote










            up vote
            3
            down vote









            Check Apple's documentation, and you find this:




            "When you request authorization, or when your app's authorization
            status changes, use the locationManager(_:didChangeAuthorization:)
            method of your delegate object to process the changes. Listing 3 shows
            an implementation of that method that enables or disables features
            based on the app's current authorization level."




            Obviously this delegate method will also be triggered if the user decides to turn off your permission to get location data.



            If you are showing this alert, you should also use this notification to dismiss the alert, in case the user went to Settings manually without using the button in your alert.






            share|improve this answer












            Check Apple's documentation, and you find this:




            "When you request authorization, or when your app's authorization
            status changes, use the locationManager(_:didChangeAuthorization:)
            method of your delegate object to process the changes. Listing 3 shows
            an implementation of that method that enables or disables features
            based on the app's current authorization level."




            Obviously this delegate method will also be triggered if the user decides to turn off your permission to get location data.



            If you are showing this alert, you should also use this notification to dismiss the alert, in case the user went to Settings manually without using the button in your alert.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 16:42









            gnasher729

            41.2k44676




            41.2k44676











            • Thank you for the comment, I have implemented the delegate methods already, the issue is that when the app enters the foreground and the user re-enters my viewController from the settings view no action is taken. The CLLocation delegate methods do not handle that.
              – Jason P.
              Nov 10 at 20:59

















            • Thank you for the comment, I have implemented the delegate methods already, the issue is that when the app enters the foreground and the user re-enters my viewController from the settings view no action is taken. The CLLocation delegate methods do not handle that.
              – Jason P.
              Nov 10 at 20:59
















            Thank you for the comment, I have implemented the delegate methods already, the issue is that when the app enters the foreground and the user re-enters my viewController from the settings view no action is taken. The CLLocation delegate methods do not handle that.
            – Jason P.
            Nov 10 at 20:59





            Thank you for the comment, I have implemented the delegate methods already, the issue is that when the app enters the foreground and the user re-enters my viewController from the settings view no action is taken. The CLLocation delegate methods do not handle that.
            – Jason P.
            Nov 10 at 20:59











            up vote
            0
            down vote













            Implement Delegate method of CLLocationManagerDelegate



            // MARK: - CLLocationManagerDelegate
            extension LocationTracker: CLLocationManagerDelegate

            func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
            print(locations)
            guard let location = locations.first else return
            lastLocation = location
            print(LocationTracker.shared.lastLocation)
            print("location = (location.coordinate.latitude) (location.coordinate.longitude)")
            locateMeCallback?(location)


            func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
            print(error.localizedDescription)


            func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
            enableLocationServices()





            /// enableLocationService goes like this



            func enableMyAlwaysFeatures() 
            locationManager.allowsBackgroundLocationUpdates = true
            locationManager.pausesLocationUpdatesAutomatically = true
            locationManager.startUpdatingLocation()
            locationManager.delegate = self






            share|improve this answer
























              up vote
              0
              down vote













              Implement Delegate method of CLLocationManagerDelegate



              // MARK: - CLLocationManagerDelegate
              extension LocationTracker: CLLocationManagerDelegate

              func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
              print(locations)
              guard let location = locations.first else return
              lastLocation = location
              print(LocationTracker.shared.lastLocation)
              print("location = (location.coordinate.latitude) (location.coordinate.longitude)")
              locateMeCallback?(location)


              func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
              print(error.localizedDescription)


              func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
              enableLocationServices()





              /// enableLocationService goes like this



              func enableMyAlwaysFeatures() 
              locationManager.allowsBackgroundLocationUpdates = true
              locationManager.pausesLocationUpdatesAutomatically = true
              locationManager.startUpdatingLocation()
              locationManager.delegate = self






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                Implement Delegate method of CLLocationManagerDelegate



                // MARK: - CLLocationManagerDelegate
                extension LocationTracker: CLLocationManagerDelegate

                func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
                print(locations)
                guard let location = locations.first else return
                lastLocation = location
                print(LocationTracker.shared.lastLocation)
                print("location = (location.coordinate.latitude) (location.coordinate.longitude)")
                locateMeCallback?(location)


                func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
                print(error.localizedDescription)


                func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
                enableLocationServices()





                /// enableLocationService goes like this



                func enableMyAlwaysFeatures() 
                locationManager.allowsBackgroundLocationUpdates = true
                locationManager.pausesLocationUpdatesAutomatically = true
                locationManager.startUpdatingLocation()
                locationManager.delegate = self






                share|improve this answer












                Implement Delegate method of CLLocationManagerDelegate



                // MARK: - CLLocationManagerDelegate
                extension LocationTracker: CLLocationManagerDelegate

                func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
                print(locations)
                guard let location = locations.first else return
                lastLocation = location
                print(LocationTracker.shared.lastLocation)
                print("location = (location.coordinate.latitude) (location.coordinate.longitude)")
                locateMeCallback?(location)


                func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
                print(error.localizedDescription)


                func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
                enableLocationServices()





                /// enableLocationService goes like this



                func enableMyAlwaysFeatures() 
                locationManager.allowsBackgroundLocationUpdates = true
                locationManager.pausesLocationUpdatesAutomatically = true
                locationManager.startUpdatingLocation()
                locationManager.delegate = self







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 at 9:50









                SuryaKantSharma

                347314




                347314



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240978%2fswift-4-refresh-view-after-updating-privacy-settings-cllocationmanager%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo