This is my code, i entered this in the viewDidLoad method; (This is my view controller)
I am following a tutorial and the source code can be found here
corelocation = [[CorelocAPI alloc] init];
corelocation.delegate = self;
[corelocation.locMgr startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog (@"Failed !"); // This never gets executed. For more details why this is happening read below
}
- (void)locationUpdate:(CLLocation *)newLocation {
NSLog(@"location - %@",[NSString stringWithFormat:@"%f",self.lat]);
}
In the CorelocAPI i have defined all these;
- (id)init {
self = [super init];
if(self != nil) {
self.locMgr = [[[CLLocationManager alloc] init] autorelease]; // Create new instance of locMgr
self.locMgr.delegate = self; // Set the delegate as self.
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationUpdate:newLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationError:error];
}
}
The problem i am having is when there is an error it executes the didFailWithError method in the
CorelocAPI class, and not the view controllers didFailWithError method. Therefore it never executes the NSLog in the didFailWithError method of the viewController class.
How can i modify my code to make the program execute the didFailWithError method of the viewController class (when an error occurs) ?
your view controller error method is wrong, it should be
locationError:(NSError *)error, notlocationManager:(CLLocationManager *)manager didFailWithError:(NSError *)errorit looks like you’re getting delegates mixed up, the view controller is a delegate of the CoreLocAPI object, which is a delegate of its CLLocationManager object