1. Import Frame
Add "Corelocation.framework" in Xcode
2. Import the main header file
#import <CoreLocation/CoreLocation.h>
3. Claims manager and Agent
@interface Viewcontroller () <CLLocationManagerDelegate> @property (nonatomic, strong) Cllocationmanager* @end
4. Initialize the manager in Appdelegate or controller
//Location ManagerSelf.locationmanager=[[Cllocationmanager alloc]init]; if(![Cllocationmanager locationservicesenabled]) {Sslog (@"the location service may not be open at this moment, please set it open! "); //return; } //Request User Authorization If no authorization is granted if([Cllocationmanager authorizationstatus]==kclauthorizationstatusnotdetermined) {[Self.locationmanager requestwheninuseauthorization];//get a location when you use the app//[Self.locationmanager requestalwaysauthorization];//Always get location } //Set up proxySelf.locationmanager.Delegate=Self ; //Set positioning accuracyself.locationmanager.desiredaccuracy=kcllocationaccuracybest; //Positioning frequency, positioning every few metersCllocationdistance distance =10.0;//10 m positioning onceSelf.locationmanager.distancefilter=distance; //Start Tracking Location[Self.locationmanager startupdatinglocation];
5. Agent method
//track the location agent method, which is executed each time the position changes (as long as it is positioned to the appropriate location)-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{cllocation*location=[locations Firstobject];//Take the first position out of theCllocationcoordinate2d coordinate=location.coordinate;//position coordinatesSelf.coordinate =coordinate; Sslog (@"Longitude:%f, Latitude:%f, Elevation:%f, heading:%f, walking speed:%f", Coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed); //If you don't need real-time positioning, use it even if you turn off location services[Self.locationmanager stopupdatinglocation];}
6. Set in the Plist file
Nslocationwheninuseusagedescription later corresponds to the user's description when used
The implementation of the IOS location feature