I have to run my app using iPhone OS 4.0. but while I am running my app the CLLocationManager delegate is not getting called.
The delegate method is
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
}
I can probably help, as I've been dealing with CLLocationManager successfully on iOS4 on a 3GS device, but I need more information.
1) Did you alloc/init the CLLocationManager previously and call startUpdatingLocation? Please include that code, we may be able to help. Specifically, where/how you are setting the delegate? Also, make sure someone is retaining your delegate class so that way you're able to receive the calls. CLLocationManager will not retain the delegate.
2) Are you testing on a device, or on the Simulator? If you are testing on a device, which device?
Related
I am using CoreLocation Framework for getting latitude and longitude in my project.
I have implement this delegate method in my .m file
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(#"Locations : %#", locations);
}
But in iOS 5.0 & 5.1, this method is not call.
and in iOS 6.0 or later it's call properly.
so how to call this method in iOS 5.0 or later.
didUpdateLocations: did not exist in ios 5 or earlier. If you want to support those ios version then just use the deprecated didUpdateToLocation:fromLocation. Even though it is deprecated it still works in ios 6 and 7. That's the simplest solution.
You could have delegate methods for both, but that gets more complicated to manage. If you don't need to build track logs in the background then you don't really need didUpdateLocations: it was created to save battery power while collecting multiple points when running in the background.
See this answer...This method for iOS 5 (deprecated on iOS 6)and old version.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
This method for iOS 6 and later version..
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
Refer:
http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html
As you can see locationManager:didUpdateLocations: is available only in iOS 6.0 and later.
You can always get the recent updated location from
CLLocationManger's location
property.
Can anyone please let me know if it is possible to handle events like OrientationChanged, Shake, LocationChanged etc.. when my App running in background?
I have tried following code but it gets invoked only when my app running in foreground!
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session
if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 120) {
self.currentLocation = newLocation;
}
NSLog(#"lat: %f long:%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
}
Thanks.
http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html
You should include location-services in your info.plist file under the key UIRequiredDeviceCapabilities
then your app will receive location updates in the background.
However this drains the battery at a faster rate..without user knowing much..If possible you should register for only significant location
orientation changed updates won't be possible in background.
shake might be..i am not sure
How can I get the CLLocationManager delegate methods, when the app gone into background,
I am new to CLLocationManager
Actually my app gets called CLLocationmanager dalegate methods when the app is running in the foreground but when the app goes into the background those are not called.
By using
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
method my app get called these methods
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(#"Entered Region");
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(#"Exited Region");
}
How can I use the CLLocationmanagerdelegate in the appDelegate class?
Have you set the background modes in the app's plist to include location services?
Take a look at the answer to my question as they are quite related.
Basically, creating a singleton CLLocationManager solved the problem.
I am using iphone simulator 4.2 and try to display or NSLog Heading and other Location services attributes e.g. Latitude, Longitude, altitude, horizontalAccuracy, VerticalAccuracy, speed. but its not showing the correct parameters and Incase of Heading its actually not firing the event.
as its execute CLLocation code
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.delegate locationUpdate:newLocation];
}
and not executing CLHeading code
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
[self.delegate headingUpdate:newHeading];
}
and when I put breakpoint on these both codes it never touch CLHeading code. I am updating location and heading in init.
- (id) init{
if (self!=nil) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
[self.locationManager startUpdatingHeading];
}
return self;
}
The problem is that I do not know that is due to simulator or there is any problem in code?
please help.
CLLocationManager needs additional hardware and hence wont work on simulator. However you can test that using the method described in this other SO question.
From the documentation:
Some location services require the presence of specific hardware on the given device. For example, heading information is available only for devices that contain a hardware compass. This class defines several methods that you can use to determine which services are currently available.
This answer can be updated for anyone using Xcode 4.2. It is still in beta status, but if you are a paid developer you will have access to it. Also, if you are a paid developer, there are some good videos from WWDC 2011 that explain how to use the simulator for location simulation.
WWDC 2011
See What's New in Core Location and Testing Your Location-Aware App Without Leaving Your Chair
**Note: Please note again that only paid developers have access to these videos
It looks like the behaviour is by default
It fires Location but not heading.
I have not tested my application in actual hard ware to confirm my though...
Anthony Desa
I'm trying to implement Application Tests as described here. So far, so good, but i fail to test, for instance, the location of the device using Core Location. I have added the appropriate Framework to the Target, and have initiated the update of location, but i have no clue of how to wait for the location to be loaded, the test suite just ends before the second thread finish. Please help me to find a way to test this sort of things.
If you're relying on CLLocationManager you can implement these two delegate methods in CLLocationManagerDelegate:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
Then you set the CLLocationmanager delegate and tell it to startUpdatingLocation. If you get didUpdateLocation (with oldLocation set to nil) it means you now have a location and you can consider the test a success (make sure you turn off updating). If you get the other one there was a location-manager error.
If you're relying on MapKit and are using MKMapView's user-location update mechanism, then take a look at my response to this question and implement the observer section (observeValueForKeyPath) to be notified when the map has a location (success) or implement MKMapViewDelegate's mapViewDidFailLoadingMap to be notified if there was an error.