self.locationManager = [[[CLLocationManager alloc] init]autorelease];
self.locationManager.pausesLocationUpdatesAutomatically=NO;
[self.locationManager setDelegate:self];
if([[[UIDevice currentDevice ]systemVersion] floatValue]>=8.0)
{
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
}
[self.locationManager startUpdatingLocation];
The code never calls didUpdate locations method instead did fail with error called immediately.
Error Description:
The operation couldn’t be completed. (kCLErrorDomain error 0.)
But some times didUpdate locations getting called (1 out of 20 times), Tried resetting Network and locations settings but still not working.
Thank you
Have you tried to take a look at my answer there ?
In short :
Product -> Scheme -> Edit Scheme -> Options -> Allow Location Simulation must be checked and try providing a default location, don't leave it set to "none"
This helped me out:
Simulator -> Features -> Location -> Custom Location...
Then set a preferred Custom Location with latitude and longitude. Whatever you do, you don't want to leave None, checked.
Related
I've problem with getting my current location. I still getting error:
Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
I already did everything that I found here:
Reset content and settings in iOS Simulator (map application in simulator shows correct location).
In Xcode at Product>Scheme>Edit I unmarked Allow Location Simulation (when I mark it,It simulate e.g. London).
I have added NSLocationWhenInUseUsageDescription to Info.plist file in my project (App asks for Location permissions and I can see them in Settings in iOS simulator).
I have Wi-Fi connection turned on (like I said location services works fine in Safari, Maps or even Maps in iOS Simulator).
-(void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER){
NSUInteger code = [CLLocationManager authorizationStatus];
if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)])) {
if([[NSBundle mainBundle] objectForInfoDictionaryKey:#"NSLocationWhenInUseUsageDescription"]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
NSLog(#"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
}
}
}
[self.locationManager startUpdatingLocation];
[self.FirstMap setShowsUserLocation:YES]; }
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show]; }
Is there something that i forgot about?
Thanks!
I guess maybe you need to read this thread first as it's largely overlapped.
Location Manager Error : (KCLErrorDomain error 0)
But anyway, I tested your code and got the same error as you reported. Then I change the simulation configuration. To be specific, in the simulator, I clicked Debug -> Location -> Custom Location... and enter a coordinate, and I can periodically receive location events (at a speed of every sample per second.) So maybe the problem is not from the code but from the simulator itself. Hope my answer helps.
UPDATE
I guess I misunderstood your question. What you want is to "automatically" get your current location in simulator. I'm afraid there's no such feature in xcode, instead, you need to use "add gpx file to project", which is even more work than using customized location. But I mean, if you've successfully got correct locations, why it bothers you to get the correct current location from the simulator, or alternative, maybe you can test on phone?
In iOS 8 you have to first take the permission from the user to access the location you cannot access the local as in iOS 7
if CLLocationManager.locationServicesEnabled() {
if self.manager.respondsToSelector("requestWhenInUseAuthorization") {
manager.requestWhenInUseAuthorization()
} else {
startUpdatingLocation()
}
}
for more take help from this link http://nshipster.com/core-location-in-ios-8/
Check Internet connection.
if connection is ok then
First Go to Xcode log bar and set "Dont simulate location". Then select simulator menu>debug>location>set custom or select any.
I have an app that uses background location updates, it needs to constantly track the devices position regardless if the app is in the foreground or background. I have the background tracking setup in the app delegate.
In the front end I have a single UIViewController with a working mapkit view all the CCLocationManager code triggers without error but didUpdateUserLocation is never fired within the custom UIViewController class.
Background location tracking is working with absolutely no problems.
Here is the code i'm using in viewDidLoad
[self.mapView setShowsUserLocation:YES];
[self.mapView setShowsPointsOfInterest:YES];
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager requestWhenInUseAuthorization];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager setDelegate:self];
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
NSString *error;
if (![CLLocationManager locationServicesEnabled]) {error = #"Error message";}
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {error = #"Error message";}
if (error) {NSLog(error);}
else
{
status = [CLLocationManager authorizationStatus];
self.locationManager.pausesLocationUpdatesAutomatically = NO;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[self.locationManager stopMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
}
Any help would be appreciated
Thanks
You need to check below details:
Testing this on simulator?? didUpdateLocations must be tested on real device.
Make sure that you specified the NSLocationAlwaysUsageDescription string or else the app won't present the authorization message, and you won't be authorized.
Have you implemented locationManager:didFailWithError:? did you got any error?
Have you implemented locationManager:didChangeAuthorizationStatus:? Are you getting this called with a successful authorization status?
I worked out the problem finally.
Turns out even if you're using the location stuff in Schema and Xcode you still need to set it in the Simulator via Debug > Location
Hopefully this helps someone else.
and now, with iOS 9, the following needs to be included for any update;
if ([ locationManager respondsToSelector:#selector(requestWhenInUseAuthorization )])
{
locationManager.allowsBackgroundLocationUpdates = YES;
}
Else background updates will be impaired.
I am trying to get authorization from user before fetching his location. I am using this code for that:
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
// iOS8+
// Sending a message to avoid compile time error
[self.locationManager requestAlwaysAuthorization];
}
[self showIndicatorView];
self.getCityService = [[GetCitiesService alloc] initServiceWithDelegate:self isLocalCall:NO];
[self.getCityService fetchCities];
I see the alert on screen but before I allow it or not, it disappear form screen and app is not authorized.
I want my code to stop until user gives permission.
Apparently in iOS 8 SDK, requestAlwaysAuthorization (for background location) or requestWhenInUseAuthorization (location only when foreground) call on CLLocationManager is needed before starting location updates.
There also needs to be NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt. Adding these solved my problem.
Hope it helps someone else.
EDIT: For more extensive information, have a look at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
You code looks really weird, since you seem to be calling the requestAlwaysAuthorization twice. Once on self.locationManager and once via the sendAction.
You code should look like:
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
// iOS8+
// Sending a message to avoid compile time error
[self.locationManager requestAlwaysAuthorization];
}
I am trying to find User's current location using Location Manager. Below is my code in viewdidload
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if(IS_OS_8_OR_LATER)
{
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
}
[locationManager startUpdatingLocation];
and in didUpdateLocations
self.userLocation = locations.lastObject;
if (userLocation != nil)
{
NSLog(#"sdsd %f %f", userLocation.coordinate.longitude, userLocation.coordinate.latitude);
[locationManager stopUpdatingLocation];
}
Am getting confirmation dialog and users location as set in simulation but not real time location even in device. I configured all settings in both device as well as xcode but still getting default location only.
Please let me know what should I do to get my realtime location and not simulation location?
The simulator does not receive your current location from the operating system. To debug you can select Debug->Location->City Bicycle Ride in the simulator. This will simulate a bike ride around the block.
On the device it can take a while to get your current location if you are indoors. Turning on bluetooth and WIFI help iOS get a location faster.
Open your xCode go to Product-> Scheme-> Edit Scheme and then here inside Run select Core location and check the Allow location simulation. May be it will work.
I am using region monitoring to run some code when the user exits a region. When the app is running in the foreground or in the background with the device's screen on locationManger:didExitRegion: is called as expected. However, when the screen is off I usually don't get the callback until I turn the screen on, by clicking the power button, at which point locationManger:didExitRegion: is called immediately. All of the testing was done on an iPhone 5s.
Here's how I set the region:
self.monitoredRegion = [self setupGeoFenceWithCenter:self.currentLocation radius:200];
[self.locationManager startMonitoringForRegion:self.monitoredRegion];
-(CLRegion *)setupGeoFenceWithCenter:(CLLocation *)center radius:(CGFloat)radius
{
if (radius > self.locationManager.maximumRegionMonitoringDistance)
{
radius = self.locationManager.maximumRegionMonitoringDistance;
}
CLRegion * region = [[CLCircularRegion alloc] initWithCenter:center.coordinate
radius:radius
identifier:#"geofence"];
return region;
}
Here's the delegate callback:
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(#"Exited Region");
}
Here's the CLLocationManager initialisation:
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
Also, lately I've noticed that the locationManager:monitoringDidFailForRegion:withError: method is getting called more often with the following error description:
Error Domain=kCLErrorDomain Code=5 "The operation couldn’t be
completed. (kCLErrorDomain error 5.)"
but even with this error, when the app is in the foreground it works as expected.
Is this the expected behaviour for region monitoring? Has anyone else encountered this? Could this be a result of the error I'm getting?
Thanks!
Did you try waiting several minutes (4 to 15) ? PS : your app should be in foreground before switch your iDevice off. This is based on iBeacon tests... hop it can help in your case.