ryan
ryan

Reputation: 1354

Objective-c iOS - Can't receive delegate methods in my custom class

My custom class needs to receive the "didUpdateToLocation" CLLocationManagerDelegate method, however i can't seem to make the following code work.

Header file.

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MyCurrentLocation : NSObject<MKAnnotation,CLLocationManagerDelegate,MKMapViewDelegate>
{
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) CLLocationManager *locationManager;

-(MyCurrentLocation *)init;

@end

Implementation file.

#import "MyCurrentLocation.h"

@implementation MyCurrentLocation

-(BLCurrentLocation *)init
{
    self = [super init];
    if (self) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [_locationManager startUpdatingLocation];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"Did we receive a location?");
}

Upvotes: 1

Views: 159

Answers (2)

J2theC
J2theC

Reputation: 4452

You have to respond to other delegate methods, such as the error callback. Also, check for the CLLocationManager's + (CLAuthorizationStatus)authorizationStatus class method to determine if you are able to use location services.

Upvotes: 1

John
John

Reputation: 2694

I don't know why but maybe you can try to implement this delegate method to see if location service cannot be registered.

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please make sure Location Service is ON" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alertView show]; 
}

Upvotes: 1

Related Questions