iPhone: Catch the events when App running in background - iphone

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

Related

MKMapview showing current location wrong

I am using MKMapView and it is showing my current location. in india it is showing well and exact what in google map but in US current location is showing wrong.
i am using
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
any help please ?
If you are using an MKMapView you can use its delegate method mapView:didUpdateUserLocation:
You need to set showsUserLocation property of your map view to YES.
Then the map view uses CoreLocation framework to determine current location.
It tracks userLocation by itself whenever it receives location updates and updates it periodically.

is iphone Simulator capable of showing heading and latitude , longitude?

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

MapView snaps back to original location - help!

I have a mapView with custom annotations and just ran into a problem on a beta tester's iPhone. The mapView won't let the user move to any location.. as soon as you try to move, it snaps right back to the original coordinates.
Any idea why? It doesn't happen in the simulator, and I notice it a little bit on my own device... but it is a consistent problem on another device.
Thanks so much!
static BOOL haveAlreadyReceivedCoordinates = NO;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if(haveAlreadyReceivedCoordinates) {
return;
}
haveAlreadyReceivedCoordinates = YES;
CLLocationCoordinate2D loc = [newLocation coordinate];
[mapView setCenterCoordinate:loc];
}
Icky put you on the right track. You shouldn't be constantly resetting the map's center to whatever you get back from the location manager. The location manager will send you all sorts of updates.
You may wish to only use the update once and then ignore future updates (by setting some kind of flag), or you may wish to center the map on the user's location only when the user presses a button. In the latter case the only thing you need to do in didUpdateToLocation: is store the new location into some member variable.
The reason you don't see this in the simulator is that the location hardware simulator isn't constantly updating the way the real one is.

location problem in IOS4

i am using location in my app and tested it with IOS 3 simulatior it works fine also on device it works fie.
but when tested it on IOS4 simulator it crashes..and unable to find location
I know that the location on simulator is of infinite loop cupertino california.
but it is not working in IOS4...
and i don't have Iphone4...So can't test further
please help
Maybe the simulator can't retrieve the default CLLocationManager location.
You can try this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
location = newLocation.coordinate;
// Manually setting a default location for simulator only
if(TARGET_IPHONE_SIMULATOR){
location.latitude= 45.00;
location.longitude= 45.00;
}
}

iPhone: Application Testing and Core Location

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.