The first thing to add is the library corelocation.framework
Then you need to add two items in the Info.plist, Nslocationwheninuseusagedescription and Nslocationalwaysuseusagedescription.
This is asking whether the current device can access location permissions,
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface Viewcontroller () < cllocationmanagerdelegate >
@property (strong,nonatomic) Cllocationmanager *locationmanager;
@property (strong,nonatomic) UILabel *lab;
@property (strong,nonatomic) UILabel *lab2;
@end
@implementation Viewcontroller
@synthesize lab,lab2;
-(void) viewdidload {
[superviewdidload];
Additional setup after loading the view, typically from a nib.
Lab = [[UILabelalloc] initwithframe: CGRectMake( +, +, )];
Lab. text = @" Longitude " ;
[self. View Addsubview : Lab ];
lab2 = [[UILabelalloc] initwithframe: CGRectMake , 50 )];
lab2. text = @" Latitude " ;
[self. View Addsubview : LAB2 ];
UIButton *btn = [[UIButtonalloc] initwithframe: cgrectmake( A. )];
[btn settitle:@ " positioning "forstate: uicontrolstatenormal];
[btn settitlecolor: [uicolorredcolor] Forstate:uicontrolstatenormal];
[btn addTarget:selfAction:@selector :) forcontrolevents : UIControlEventTouchUpInside ];
[self. View Addsubview : btn];
// instantiation of
}
-(void) Location: ( ID ) Sender
{
// Determine if location services are available
if ([cllocationmanagerlocationservicesenabled]) {
NSLog(@ "Star location");
// set positioning accuracy for best accuracy
Self.locationManager.desiredAccuracy = Kcllocationaccuracybest;
// Set the distance filter to ten meters, indicating that the data is updated once per move
Self.locationManager.distanceFilter = 50;
self. Locationmanager = [[cllocationmanageralloc] init];
self. Locationmanager . Delegate = Self ;
// Access can be positioned to focus!!!!!!!!!!!!!!!!!!! For use with info.plist, there must be
if ([self. Locationmanager Respondstoselector : @selector (requestwheninuseauthorization)]) {
[self. Locationmanager requestwheninuseauthorization ];
[self. Locationmanager requestalwaysauthorization ];
}
// start listening position
[self. Locationmanager startupdatinglocation ];
}
Else
{
NSLog(@ "Unsupported");
}
}
#pragma Mark Cllocationmanagerdelegate
-(void) Locationmanager: ( Cllocationmanager *) Manager didupdatelocations: (nsarray*) Locations
{
NSLog(@ " get data ah ");
// get last location data
cllocation *location = [Locations lastobject];
// get longitude, latitude, altitude, speed, direction, etc.
lab2. text = @ "DDD" ;
Lab. text = [nsstringstringwithformat:@ "%f", location. coordinate . Longitude ];
lab2. text = [nsstringstringwithformat:@ "%f", location. coordinate . Latitude ];
}
// update data in real time
-(void) Locationmanager: ( Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status
{
//NSLog (@ " get data ah ");
}
-(void) Locationmanager: ( Cllocationmanager *) Manager didfailwitherror: (nserror *) Error
{
NSLog(@ "Failed");
}
-(void) didreceivememorywarning {
[superdidreceivememorywarning];
//Dispose of any resources, can be recreated.
}
@end
Code about native API targeting