0

I want to get users current location. Here is my code

    // In LoginViewController.h

@interface LoginViewController : UIViewController <UserLocationDelegate,UIAlertViewDelegate> {

    CLLocation *usersLocation;
}
@property (nonatomic,strong) CLLocation *usersCurrentLocation;
@end


// In LoginViewController.m

@implementation LoginViewController

@synthesize usersCurrentLocation;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.usersCurrentLocation = nil;
    [self currentLocationOfUser];
}

-(void)currentLocationOfUser {

    UserLocation *userLocation = [[UserLocation alloc]init];//UserLocation];
    userLocation.delegate = self;
    [userLocation getCurrentLocationOfUser];
}

#pragma mark - User Location Delegate Methods

- (void)locationUpdate:(CLLocation *)location
{
    self.usersCurrentLocation = location;
    NSLog(@"Latitude:- %.6f",self.usersCurrentLocation.coordinate.latitude);
    NSLog(@"Longitude:- %.6f",self.usersCurrentLocation.coordinate.longitude);

}

- (void)locationError:(NSString *)errorMsg
{
    [Common showAlertWithTitle:@"Error" andMessage:errorMsg];
}


// In UserLocation.h


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

@protocol UserLocationDelegate <NSObject>

@required
- (void)locationUpdate:(CLLocation *)location; 
- (void)locationError:(NSString *)errorMsg;

@end

@interface UserLocation : NSObject <CLLocationManagerDelegate> {

    CLLocationManager *locationManager;
    __weak id<UserLocationDelegate>delegate;

}

@property (nonatomic,strong) CLLocationManager *locationManager;
@property (nonatomic,weak) id<UserLocationDelegate>delegate;

-(id)initUserLocation;
-(void)getCurrentLocationOfUser;

@end

// In UserLocation.m

#import "UserLocation.h"

@implementation UserLocation

@synthesize locationManager;
@synthesize delegate;
@synthesize geoCodingDelegate;

-(id)initUserLocation 
{
    if (self == [super init]) {
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    }
    return self;
}

-(void)getCurrentLocationOfUser {

    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    if ([CLLocationManager locationServicesEnabled]) {
        //[self.locationManager startMonitoringSignificantLocationChanges];
        [self performSelector:@selector(startLocationUpdate) withObject:nil afterDelay:2.0];
    }
    else {
        if ([delegate respondsToSelector:@selector(locationError:)]) {
            [self.delegate locationError:@"Please Turn On Location Services in Settings To Retrive User's Current Location"];
        }
        else {
            [Common showAlertWithTitle:@"Error" andMessage:@"Please Turn On Location Services in Settings To Retrive User's Current Location"];
        }
    }
}

-(void)startLocationUpdate
{
    [self.locationManager startMonitoringSignificantLocationChanges];
}

#pragma mark CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    if ([delegate respondsToSelector:@selector(locationUpdate:)]) {
        [delegate locationUpdate:newLocation];
    }
}

-(void)locationManager:(CLLocationManager *)manager 
      didFailWithError:(NSError *)error
{
    if ([delegate respondsToSelector:@selector(locationError:)]) {
        [delegate locationError:@"Some Error Occured While Retriving Users's Location"];
    }
    else {
        [Common showAlertWithTitle:@"Error" andMessage:@"Some Error Occured While Retriving Users's Location"];
    }
}

@end

But it is not returning any location update. Also its not asking user permission to use location. My app is not listed in Location Services. How can I add my app in Location Serverices ? What's worong in above code ?

1 Answer 1

0

I solved this problem by declaring UserLocation *userLocation in .h & writing property. Since I am using ARC , its deallocating userLocation so I am not getting location updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.