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:
AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:
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
add a comment |
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:
AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:
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
add a comment |
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:
AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:
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
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:
AFTER USER CLICKS BACK TO APP, PAGE NEEDS TO REFRESH TO UPDATE LOCATION ON MAP:
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
ios swift cllocationmanager
asked Nov 10 at 16:27
Jason P.
23617
23617
add a comment |
add a comment |
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()
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
add a comment |
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.
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
add a comment |
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
add a comment |
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()
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
add a comment |
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()
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
add a comment |
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()
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()
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 14 at 9:50
SuryaKantSharma
347314
347314
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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