Cannot get user's location with NSLocationWhenInUseUsageDescription - cllocationmanager

I'm using Xcode 6 beta (6A215l). I've referred this question and updated my code to:
use NSLocationWhenInUseUsageDescription in the .plist file,
call requestWhenInUseAuthorization in the source code
but neither didUpdateLocations: nor didFailWithError: was called. If I use NSLocationAlwaysUsageDescription and requestAlwaysAuthorization, my app works.
Does anyone have the same problem? I wonder if I should try with Xcode 6 beta 2..

In fact, if you want to use the requestWhenInUseAuthorization in your app and gets the didUpdateLocations: and didFailWithError: delegate methods to be called, you need to set in your .plist file BOTH NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription keys, to make it work.
Theses texts will then be displayed in the Settings/Privacy/Location Services/App section of your device.

Related

Xamarin iOS: Check if Google fit app is installed on the device

Is there any way to check if 'Google Fit' app is installed on iOS device?
I tried:
UIApplication.SharedApplication.CanOpenUrl(new NSUrl("FitApp://com.google.fit"));
this always returns false
You need to add FitApp to the LSApplicationQueriesSchemes array in your Info.plist (note, there is no colon).
Then, in code your can check to see if you can open FitBit: - note the colon
UIApplication.SharedApplication.CanOpenUrl(new NSUrl("FitApp:"));

Not being prompted to enable Location Services in App

UPDATE: THIS IS NOT A DUPLICATE. I have already added the required key to the info.plist as stated in my original question and the issue remains. I have tried all three keys in various combinations.
Before anyone gets upset, I have read through many Apple Dev forum posts and stack overflow posts and cannot figure out why my app refuses to prompt the user to allow When In Use authorization.
I've added the following key to my Info.plist file with an accompanying String value:
NSLocationWhenInUseUsageDescription
I've then written (both in Swift and Obj-C) the code that should prompt the user:
#property CLLocationManager *location;
...
#synthesize location;
...
location = [[CLLocationManager alloc] init];
location.delegate = self;
location.desiredAccuracy = kCLLocationAccuracyBest;
location.distanceFilter = kCLDistanceFilterNone;
[location requestWhenInUseAuthorization];
[location startUpdatingLocation];
I'm using the following CLLocationManagerDelegate methods.
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
This was basically copied straight from the Apple "LocateMe" sample code.
No matter what various sequences or minor changes I try, the app never prompts to allow authorization. I've used a switch statement to determine the state of [CLLocationManager authorizationStatus], but continually recieve a "Not Determined" response.
if ([CLLocationManager locationServicesEnabled]) {
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(#"Always Authorized");
break;
case kCLAuthorizationStatusDenied:
NSLog(#"Denied");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(#"Authorized in Use");
break;
case kCLAuthorizationStatusNotDetermined:
NSLog(#"Not Determined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(#"Restricted");
break;
}
}
Any help would be greatly appreciated. I'm running Xcode 6.2 (6C101) with iOS 8.1.2 physical device and iOS 8.2 (12D5452a) simulator for testing.
I faced the exact same problem, after digging in for two days, it turned out that the app does not read from the info.plist file that is included in the bundle in project navigator's pane, because it has been localized to another language and base internationalization is being used, that means in Finder there will be 3 info.plist files:
1- under Tests folder
2- under base.lproj folder
3- under ar.lprj (ar refers to Arabic language).
the project reads from base.lproj's version which is not included inside the bundle in the project navigator's pane.
what I did is that I took a backup copy of the plist file that I want (number 2 above), and removed localization on the info.plist file that is inside project navigator and chose delete from disk to delete it completely, then I took the back up copy and placed in under project's root in Finder and import it back in Xcode, and now the project reads from the new info.plist file and the key NSLocationWhenInUseUsageDescription fires up the user authorization alert at app launch.
hope this helps.
If you have changed the Location Settings in your iPhone simulator this may cause the prompt to not show. For example, if you are already set the Location permissions via Allow Locations access iOS prompt and made a decision this is stored in the Privacy settings for the iPhone simulator and so subsequent calls to locationManager.requestAlwaysAuthorization() or locationManager.requestWhenInUseAuthorization() will not display the iOS prompt because you already have defined your Privacy -> Location Settings for your app.
To clear settings for an iPhone simulator: "iOS Simulator" -> "Reset Content and Settings..."
Also I did a Clean build in Xcode and quit and restarted the iPhone simulator at some point, I could be wrong about this part being of any help...
Lots of useful comparisons here: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
Also, silly question time: you mix _location and self.location but you don't show your property/synthesize code. Are you sure you're operating on the same object?
I found that in your info.plst file you can't use the Privacy - Location Usage Description key, you have to have a key called NSLocationWhenInUseUsageDescription and a value. I was able to get your code working by adding this key into my info.plist
#import <MapKit/MapKit.h>
#interface ViewController ()
#property CLLocationManager * location;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_location = [[CLLocationManager alloc] init];
self.location.delegate = self;
self.location.desiredAccuracy = kCLLocationAccuracyBest;
self.location.distanceFilter = kCLDistanceFilterNone;
[_location requestWhenInUseAuthorization];
[self.location startUpdatingLocation];
}
It may be silly, but i just spent almost 2 hours double and triple checking all of the answers i could find about CLLocationManager permissions.
I felt stupid when i finally realized that i had 2 info.plist files and i was using the wrong one!
You can check in your project's build settings - there's a value called Info.plist File.
Just want to leave it here for future readers.

iOS8 - Simulator - How to get Locations simulated

My App tracks your location.
With iOS8 I had to change how it was starting location services.
I got this working with
[self.locationManager requestAlwaysAuthorization];
and adding NSLocationAlwaysUsageDescription to the plist file.
The app now gets your location when run on a device, but not in the simulator.
Any clues on how to get this to work in the simulator? It used to!!!
In the iOS Simulator, select Debug > Location, then choose your location.
I decided to post this in case anyone else had the same issue. I got the simulator to respond to location changes again by adding NSLocationWhenInUseUsageDescription back into the plist file. I had removed it since my app needs the Always permission instead of WhenInUse. By adding this back to the plist the simulator is now happy. I did not have this issue on the physical device, just the simulator.

Buzztouch geo location not working compiled in xcode 6

I upgraded to xcode 6, compiled a new app, discovered that the geo location is not working, and am searching for a work around.
There is no option in the phone's settings for the newly compiled app to choose a privacy/location services option "Never" or "always.
The older apps compiled in xcode5 still have this option.
UPDATE
Problem solved. Compiled in BT 3.0
- Added this code in BT_screen_customURL.m under the //appDelegate comment (line 64)
if ([appDelegate.rootLocationMonitor.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)])
[appDelegate.rootLocationMonitor.locationManager requestAlwaysAuthorization];
Added these as string messages to info.plist
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
Additonal informaton: https://www.buzztouch.com/forum/thread.php?tid=DA308929DBC29D1DB83B7A8&currentPage=1
Also, the link provided by Alberto below was helpful.
Your app is write in Objective C?
add the following lines:
kCLAuthorizationStatusAuthorizedAlways <- For background battery-killers.
kCLAuthorizationStatusAuthorizedWhenInUse <- For sensible apps.
in your plist
For more information: http://www.splinter.com.au/2014/09/14/ios8-corelocation/
Best,
Alberto

didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"

I want to get the current location, but instead I get an error.
This is a snippet of my view controller.
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
// I would get the latest location here
// but this method never gets called
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(#"didFailWithError: %#", error);
}
I'm expecting the delegate method locationManager:didUpdateLocations: to get called, but instead, only locationManager:didFailWithError: gets called, and prints this:
didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
If you are using the simulator:
Press command + shift + , in Xcode to open the scheme editor
Select the Run scheme
Go to the Options tab
Check ✅ Allow Location Simulation
Select a Default Location in the dropdown
Selecting None as your Default Location may have caused the problem.
Check that you actually have a valid WiFi and 3G connection
...if you do, then:
Go to settings and reset your location services
Reset your network settings
This should take care of that issue. It is device/network related not app related. It's annoying especially if your app is dependent on location services and the device is WiFi only and people give negative ratings on the AppStore... Pardon me, I'm getting a bit emotional there.
Simply ignore this error and wait for more updates or a different error.
The docs for the location manager say that this error is temporary. It's letting you know that it failed to immediately retrieve a location, but it continues to try. "In such a situation, you can simply ignore the error and wait for a new event." Which is a really boneheaded way to use a method named locationManager:didFailWithError: - Thanks Apple!
Apple Documentation
Try restarting the simulator (assuming that's what you're using).
After trying everything else this worked for me.
A restart of the simulator didn't work for me.
I had to clear everything via "iOS Simulator" >> "Reset Content and
Settings....
I was getting this error on the simulator. Clicking the location button in the debugging panel and setting a location fixed the issue for me. (Ensure that the button is blue)
I have the same problem. I believe the possible explanations / fixes are covered in this SO post.
Changing the "Location" on the simulator worked for me.
Debug > Location > (Mine was checked None instead of City Bicycle Ride
e.g.)
In the simulator go to Settings > General > Reset > Reset Location & Privacy
Quit simulator and run app again
Assuming that you are using the simulator, you can to Debug -> Location and set a location.
If you have none selected you will have this error.
If you are using a custom location make sure you have the long and lat the correct way, i had it reversed and wasted 3 hours before i realised...
I have see the problem before,there is a way to solve it,but it can only work one time.If you want to run the project again and you need to repeat the solution each time.
1.In Xcode,Product -> Scheme -> Edit Scheme,then cancel the "Allow Location Simulator".
2.To the iOS Simulator and reset Content and Setting.
3.Again to the Xcode,repeat the first step.
4.To the iOS Simulator and reset. Then it will work.
If you are using the Simulator, make sure the scheme allows location, and you picked a default location.
Issues like this can be resolved by setting location as "Apple". At least it works for testing purpose.
I have reseted contents and settings.
Removed data from derived data.
Restarted the Xcode, and Simulator and worked for me.
from the answer of #Mem and many others thanks
For returned devs :) Just select "Debug->Location->Freeway Drive" from the Simulator menu. If still problem then try after doing "Simulator->Reset Content and Settings" form the simulator menu. This helped me once with the same issue. Some time simulator location is set to "Custom location" due to which its not detecting anything.
Try in device. Sometimes simulator fails to take your location.
Go to home in simulator
home ->Settings ->Privacy ->Locations -> select the app and choose always
when adding permission Privacy - Location When In Use Usage Description in the info.plist it was solved for me

Resources