There is a question Stackoverflow question talking about this but I tried adding the string in Info.plist and asked for locManager.request..InUse.... When I launch the app, I can see the alert view poping up but before I do anything(allow or do not allow), it gets dismissed and there is a warning message printed out in debug console.
a basic code skeleton is like the following:
let locManager = CLLocationManager()
locManager.requestwheninuseauthorization()
// code to ask the map to track user location
Related
I want to get location updates of user even after the app is terminated from background mode or if its in the background mode. It has to work for both the cases.please provide any demo link for the same.
You can't get the location update in terminated mode.
Please see : - Getting User Current Location
For getting location in background please use the following code to authorization
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestAlwaysAuthorization()
Authorization alert will be appear like this.
Select Always allow for getting location in background mode.
I have followed the apple developer guide on location services.
I have included the info.plist key value pair:
key: Privacy - Location Always Usage Description
value: The application myTestApp needs access to location services even in the background
I have created an instance of CLLocationManager as a class variable of the view controller:
var locationManager = CLLocationManager()
I have code in the view controller viewDidLoad() function that assigns the delegate and checks for the current status:
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
let status = CLLocationManager.authorizationStatus()
switch status {
case .denied, .restricted, .notDetermined, .authorizedWhenInUse :
print("The switch detected a state other than always")
locationManager.requestAlwaysAuthorization()
default:
print("Services Authorized")
}
Yet, when I build and run the application after making small changes it does not prompt for user access, and even worse, when it does sometimes work it thinks the user is in the middle of the Atlantic ocean at Lat: 0.0 and Long: 0.0.
Is there anything outside of the code I need to do so that I can recreate the user experience of authorizing the application and then seeing it zoom to a real location?
The user permission prompt is one time only. Once it is allowed,
device would fetch user location automatically from that point.
Try setting the custom location in the simulator via these two methods.
1. Using the simulator menu actions
2. Using the Xcode debugger options
The simulator handles location a bit differently from the device. I'd suggest pushing the app over to the device itself and running from there to see how it asks for permission and make sure it is giving the right location. It's very easy to do and very helpful for testing with apps that use access to location services.
https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/LaunchingYourApponDevices/LaunchingYourApponDevices.html
Essentially, plug phone in, select it from the list of devices to simulate with, press play and accept your developer access on the phone to run the app from your device.
This question already has answers here:
How can I prompt the user to turn on location services after user has denied their use
(11 answers)
Closed 6 years ago.
I'm new to ios, but have the following question.
When the app starts, the user should accept the location permission. This works fine. If he doesn't allow, some features don't work.
However, I want it that when the user tries to use these features, he gets an alert that he didn't accept the location permission. This I also did. However I want the alert to have a button which will allow him to accept it now (so he doesn't have to go into settings on his phone to do it) This I couldn't manage to do.
I made a handler for the alert like this, but it doesn't work to accept the permission.
Here is the handler...
func someHandler(alert: UIAlertAction!) {
print("User now wants to let location from new")
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
Is this wrong, What else should I put it. Currently, when the handler is called the location services are not turned on.
Thanks
You can't do what you want. Only the user can change the setting by going to the Settings app.
If you could programmatically change the permission, it would be a security nightmare. And there is no API to force iOS to display the permission alert a 2nd time.
I'm having strange problem with UITableViewController. I'm requesting location service authroization inside viewDidLoad method, and I could see alertbox appear. However, this alertbox stays appear for couple of seconds then disappears. Why is this happening?
override func viewDidLoad() {
let locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
}
I tried this code inside the UIViewController that loads this UITableViewController and encountered different problem. This time, only push notification service request alertbox is displayed and location service request is entirely ignored. I'm guessing it is because push notification service request is not from me but from ios, and my location service request got overwritten by the ios notification request. It's my assumption correct or is there any explination for this behaviour?
Your location manager is going out of existence because it is a local variable. Make it a persistent property:
let locationManager = CLLocationManager()
override func viewDidLoad() {
// ...
}
Also I'm not sure whether it's a good idea to do this in viewDidLoad. The view is not yet in the interface; in fact, there may be no interface yet.
But there are a lot of other things wrong with your code. You may already have authorization, or authorization may already have been denied, in which case there is no point requesting it. In general authorization requests are a much more elaborate business than your simple-minded code makes out.
I'm making a Notification Center for jailbroken iPhones using the theos templates (so it runs on WeeApp). It's a weather widget, and I want it to be able to get the phone's current location so it can get weather from the closest station. I'm currently using the following code to start getting locations:
i_locationManager = [[CLLocationManager alloc] init];
i_locationManager.delegate = self;
i_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
i_locationUpdated = NO;
[i_locationManager startUpdatingLocation];
and I have a didUpdateLocations method. All of that works fine. The problem is with the pop-up the phone uses to get permission to use the location. First of all, it says that SpringBoard wants to use the location. Is there any way to get it to say my widget's name instead?
More importantly, the saved permissions don't stick. They last as long as the phone is running, but every time I respring and open the Notification Center again, it re-asks for permission to use the location.
This isn't a fatal issue, of course, but it's irritating. Is there any way to get the phone to remember that the widget is allowed to use the current location?
This might help
[CLLocationManager setAuthorizationStatus:YES forBundleIdentifier:#"com.apple.springboard"];
This will authorize SpringBoard programmatically. First time you can display pop-up and save somewhere that user authorized you. Then you can do it yourself everytime you need location.
As for application name in pop-up. You can try hooking UIApplication, SBApplication, NSBundle methods that return application name. I don't think there is easier way to do it.