i am check GPS location is on/off to use this code
BOOL servicesEnabled = [CLLocationManager locationServicesEnabled];
it work correctly when on/off LocationServices in iphone,but i am set off LocationServices for my app it return only servicesEnabled to YES,that time [CLLocationManager locationServicesEnabled] how to check locationServicesEnabled in my app
Ex: i am disable my app location services like as instagram, how to solve this problem to check LocationServices in particular app in iphone or ipad?
Use your CLLocationManagerDelegate's locationManager: didFailWithError: method and check for a kCLErrorDenied error to see if the user denied location services.
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500;
[locationManager startUpdatingLocation];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"%#",error);
}
if location service disable for your app then its give you error
Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"
Related
In my app, I implemented Cllocation manager to use current user location & it works fine. But when the app enter in background or terminate,the GPS location icon is hide automatically in ipod. When i try same with Iphone, the icon is not hide.
So, i can't find any solution for it. Help me!!
My code is as follow:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if(locationManager == nil)
locationManager =[[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy= kCLLocationAccuracyBest;
self.locationManager.distanceFilter= 5;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
self.userLocation=newLocation;
[self.locationManager startMonitoringSignificantLocationChanges];
}
Map.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[APPDELEGATE.locationManager startUpdatingLocation];
}
- (void)viewWillAppear:(BOOL)animated
{
self.map.delegate = self;
[self.map setShowsUserLocation:YES];
}
if i manually on location service for my app in ipod,when the app is closed, it not shown location icon. But when i try for the same in iphone,it shows location icon.
finally i solved this problem like below.
Instead of
[self.locationManager startMonitoringSignificantLocationChanges];
In appdelegate
i write,
[self.locationManager stopMonitoringSignificantLocationChanges];
I think what you really want is:
[self.locationManager stopUpdatingLocation];
directly afterward, if you want the manager to wake up on a significant location change, then call:
[self.locationManager startMonitoringSignificantLocationChanges];
The opposite applies to go back to continuous updating:
[self.locationManager stopMonitoringSignificantLocationchanges];
[self.locationManager startUpdatingLocation];
Note: When in Significant Location Change mode, the icon in status bar will still show the same as when updating location continuous. This is not an issue for concern as in the conservative mode it is still shutting the location services down when unneeded.
The ipod does not support startMonitoringSignificantLocationChanges. That is why the indicator behaves differently on the two devices.
You should consider that, as you have it implemented, you are using whatever the first event reports for location out of the manager. This can often be stale, cached, old or very likely horribly inaccurate. It is standard practice to use a bit of testing and give a chance for more suitable location info as needed for your purpose.
See my handler on Github for more help as you work with location services.
I'm new to iOS programming and I need to implement a location aware application. I can already use the significant location changes service, however when I leave my view it stops receiving new updates.
If I enter background on that view I still get the updates and everything is fine, but if I change my view to other it stops...
I think it is logical to happen this way but I need to receive updates on my other views as well...
Should I replicate the code for each view I have or can I make it that I receive the update in whatever view I am, like making my application answer instead of each view.
Thanks,
GustDD
Add this code in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
if(!locationManager) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
}
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//here you will get upadated location
//here you can add delegate method call to where you want to use this location or you can create a shared variable
}
You could :
define the location instance in the application delegate so you can access it anytime.
use NSNotificationCenter to post a notification when the location change.
use delegates to pass info when the location changed.
Currently, developing an app that needs to get last location from CLLocationManager (without any regular tracking). It doesn't matter how old, accurate it is. I don't need and want to start tracking - I just need to just grab some last location from a cache and that's it. IMHO, CLLocationManager is a shared component in iOS and if some app uses location tracking then another app should be able to use the most recent location from CLLocationManager.location. It should be sufficient just to alloc/init CLLocationManager and grab its location. However it's not. I have tested on iPhone4 - started google maps, saw my current location, then went to my app, but after [[CLLocationManager alloc] init] location property is nil.
UPDATE: tried [locationManager startUpdatingLocation]; and [locationManager stopUpdatingLocation]; but the result is the same. I guess, the only solution is to start regular tracking?
UPDATE2: Strange but there's no alert with "The app wants to use location services" after alloc/init of CLLocationManager. Here's my code fragment:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
[locationManager stopUpdatingLocation];
NSLog(#"%#", locationManager.location); //prints nil
First you should check if your locationManager has a, let's say, 'static' location pre-saved.
If it does, you're done.
If not, you should startUpdatingLocation and then, in the didUpdateToLocation:fromLocation: callback, stopUpdatingLocation once you get the location.
My experience says that's the best way to get only one location.
UPDATE TO MATCH AUTHOR UPDATES:
You should not stopUpdatingLocation just after startUpdatingLocation. startUpdatingLocation starts a service in background, so you should wait until you get a location, so invoque it in the callback methods.
To make any use of CLLocationManager you need to implement CLLocationManagerDelegate somewhere.
-[CLLocationManager startUpdatingLocation] starts an async process. If you stop it in the same runloop cycle the process never gets started and that is the reason you never see the permission dialog.
It goes something like this:
#interface MyClass : NSObject <CLLocationManagerDelegate> {
CLLocationManager *manager;
CLLocation *lastLocation;
}
#end
#implementation
- (id)init {
self = [super init];
if (self) {
manager = [[CLLocationManager alloc] init];
manager.delegate = self;
[manager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
{
lastLocation = newLocation;
[manager stopUpdatingLocation];
}
// in your real implementation be sure to handle the error cases as well.
#end
I'm making an app with augmented reality.
Basically what I have is a screen with image from camera, radar view and some markers on the screen.
I'm using CLLocationManager to get updates on my heading and location.
I need heading updates always turned on, because the radar rotation depends on it. I'm using location updates to get my position and based on that I'm looking for markers to show.
The problem is that when I've got both turned on the iPhone is overheating a little over some time (10 +- minutes). I've got the accuracy set to every 100 metres and distance filter to 100.
Should I set up a timer to stop and start updating of location to prevent the overheating (and probably save some battery life too)? Is there any other approach for this?
Thanks for advices.
This is a really good time to user the "Time Profiler" instrument. I'm sure using GPS is responsible for some of the heat but you could easily have some other part of the code that is using the CPU or GPU too heavily.
There's also a OpenGL performance tool, I would look at that to see if you can find any parts that are taxing the GPU too much.
Another possibility is to look into using AVFoundation to get the camera image, and see if you can specify less expensive preview parameters for the video image you are overlaying.
- (id) init
{
self = [super init];
if (self != nil)
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Location: %#", [newLocation description]);
[self.delegate locationUpdate:newLocation];
}
- (void)locationManager:(CLLocationManager *)managerdidFailWithError:(NSError *)error;
{
[self.delegate locationError:error];
NSLog(#"Error Error: %#",[error description]);
}
These delegate function will b call in MyCLConrolleer.m class.
Allocate this class in ViewController class like this.
locationController = [[MyCLController alloc] init];
locationController.delegate = self;
[locationController.locationManager startUpdatingLocation];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
showCurrentLocation = NO;
Delegate functions in View Controller class
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self getLocationNameWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
}
I am using iPhone sdk 4.0.I have used below code for taking Current location but it's delegate methods is not called Automatically...My code is given below:
in .hfile
i have import
CoreLocation/CoreLocation.h
also delegate
CLLocationManagerDelegate
CLLocationManager *locationManager;
in .mfile
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
but i don't get the current location for that......My Delegate Method is which is not called:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
}
Your code looks correct to me. Did you also tried to run this code on a real iPhone? Simulator is not always responding to location manager updates.
There is also another important delegate method you should add to your .m file:
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
This one will be called in case something goes wrong. For instance the location manager failed to localize your iPhone.
I encounter same problem. All the code looks ok but the delegate method was not called.
I closed the simulator completely. When I rerun the app, delegate method was called.