we can easily locate the user's current location in ios, provided the user allows the application to access the information with the help of the core location framework.
location handling – steps involved
step 1 − create a simple view based application.
step 2 − select your project file, then select targets and then add corelocation.framework as shown below −
step 3 − add two labels in viewcontroller.xib and create iboutlets naming the labels as latitudelabel and longitudelabel respectively.
step 4 − create a new file by selecting file → new → file... → select objective c class and click next.
step 5 − name the class as locationhandler with "sub class of" as nsobject.
step 6 − select create.
step 7 − update locationhandler.h as follows −
#import <foundation/foundation.h>
#import <corelocation/corelocation.h>
@protocol locationhandlerdelegate <nsobject>
@required
-(void) didupdatetolocation:(cllocation*)newlocation
fromlocation:(cllocation*)oldlocation;
@end
@interface locationhandler : nsobject<cllocationmanagerdelegate> {
cllocationmanager *locationmanager;
}
@property(nonatomic,strong) id<locationhandlerdelegate> delegate;
+(id)getsharedinstance;
-(void)startupdating;
-(void) stopupdating;
@end
step 8 − update locationhandler.m as follows −
#import "locationhandler.h"
static locationhandler *defaultmanager = nil;
@interface locationhandler()
-(void)initiate;
@end
@implementation locationhandler
+(id)getsharedinstance{
if (!defaultmanager) {
defaultmanager = [[self allocwithzone:null]init];
[defaultmanager initiate];
}
return defaultmanager;
}
-(void)initiate {
locationmanager = [[cllocationmanager alloc]init];
locationmanager.delegate = self;
}
-(void)startupdating{
[locationmanager startupdatinglocation];
}
-(void) stopupdating {
[locationmanager stopupdatinglocation];
}
-(void)locationmanager:(cllocationmanager *)manager didupdatetolocation:
(cllocation *)newlocation fromlocation:(cllocation *)oldlocation {
if ([self.delegate respondstoselector:@selector
(didupdatetolocation:fromlocation:)]) {
[self.delegate didupdatetolocation:oldlocation
fromlocation:newlocation];
}
}
@end
step 9 − update viewcontroller.h as follows where we have implemented the locationhandler delegate and create two iboutlets −
#import <uikit/uikit.h>
#import "locationhandler.h"
@interface viewcontroller : uiviewcontroller<locationhandlerdelegate> {
iboutlet uilabel *latitudelabel;
iboutlet uilabel *longitudelabel;
}
@end
step 10 − update viewcontroller.m as follows −
#import "viewcontroller.h"
@interface viewcontroller ()
@end
@implementation viewcontroller
- (void)viewdidload {
[super viewdidload];
[[locationhandler getsharedinstance]setdelegate:self];
[[locationhandler getsharedinstance]startupdating];
}
- (void)didreceivememorywarning {
[super didreceivememorywarning];
// dispose of any resources that can be recreated.
}
-(void)didupdatetolocation:(cllocation *)newlocation
fromlocation:(cllocation *)oldlocation {
[latitudelabel settext:[nsstring stringwithformat:
@"latitude: %f",newlocation.coordinate.latitude]];
[longitudelabel settext:[nsstring stringwithformat:
@"longitude: %f",newlocation.coordinate.longitude]];
}
@end
output
when we run the application, we'll get the following output −