iPhone: Application Testing and Core Location - iphone

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.

Related

didUpdateLocations delegate method not call in iOS 5.1 and earlier

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.

Does a MKMapView have its own location Manager?

There is a CLLocationManager i have created, and receiving the gps updates from that , but my doubt is whether the mapview has its own locationmanager, in that case there will be two location managers right? Whether this will affect my application? in case its having its own location manager
MKMapView has its own location manager.That's why you can directly show user location on map like this myMapView.showsUserLocation=YES;
Why are you using separate location manager im MKMapView? You can capture current user location by this way.
CLLocationCoordinate2D location = [[myMapView userLocation] location].coordinate;
double currentLat = location.latitude;
double currentLong = location.longitude;
Also I dont think using separate location manager in MKMapView cause any problems.
Yes, it does. And it shouldn't effect your application as long as you choose one and stick with it. Although, I will point out that having two (or one empty) is terrible coding practice. Use the following function to access your MKMapView's locationManager:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

iPhone: Catch the events when App running in background

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

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.

CLLocationManager Delegate changes in iPhone OS 4.0

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?