Corelocation Framework App Locator

Source: Internet
Author: User

#import "ViewController.h"

Import the Corelocation library after importing its header file

#import <CoreLocation/CoreLocation.h>

starting with IOS8 , the Framework's import can also be used in this way

@import corelocation;

@interface Viewcontroller () <cllocationmanagerdelegate>

@property (nonatomic, strong)Cllocationmanager *lm;

@end

@implementation Viewcontroller

Lazy Loading

-(cllocationmanager *) LM

{

if(! _LM)

{

// Locate the class to use

_lm = [[cllocationmanager alloc] init];

}

return _lm;

}

-(void) viewdidload {

[Super viewdidload];

}

-(void) Touchesbegan: (nsset<uitouch *> *) touches withevent: (uievent * ) Event

{

self. LM. Delegate = self;

// request the foreground location authorization ( when the user is using the APP to have permission to use the location permission )

[self. LM requestwheninuseauthorization];

// Request for pre-and post-location authorization .

[Self.lm requestalwaysauthorization];

// version adaptation

if ([[[[ uidevice currentdevice] systemversion] floatvalue] >= 9.0) {

// starting from iOS9 if you want to use background positioning , you must implement this property to YES

// if the requested location permission is located in the foreground and needs to be located in the background , you can check the background mode in capabilities , but there will be a blue bar appearing

self. LM. Allowsbackgroundlocationupdates = YES;

}

// version adaptation method 2

if ([Self respondstoselector: @selector (setallowsbackgroundlocationupdates:)])

//    {

[Self.lm Setallowsbackgroundlocationupdates:yes];

//    }

/*

Kcllocationaccuracybestfornavigation navigation and positioning

Cllocationaccuracy kcllocationaccuracybest Most accurate positioning

Cllocationaccuracy kcllocationaccuracynearesttenmeters within ten meters

Cllocationaccuracy kcllocationaccuracyhundredmeters within a metre

Cllocationaccuracy Kcllocationaccuracykilometer within 1 km

Cllocationaccuracy Kcllocationaccuracythreekilometers within 3 km

*/

// positioning accuracy . The more accurate the positioning accuracy , the more power consumption

self. LM. Desiredaccuracy = kcllocationaccuracybest;

// position filter , when the user position is moved more than ten meters , positioning again, if the position is stationary or moving less than ten meters is not positioned .

Self.lm.distanceFilter = 10;

// start requesting user location , This method will start at the worst level of positioning accuracy , if it is located within the timeout period, then return the positioning information , if not positioned to the set positioning accuracy to reach the time-out period , the precision location information that is currently anchored is returned . If the user has not decided whether to allow or not locate any location information after the timeout, the locationmanager:didfailwitherror will be called:

// But this method cannot be used concurrently with startupdatinglocation and must implement the Protocol method Locationmanager: Didfailwitherror:

[Self.lm requestlocation];

// start update location

[self. LM startupdatinglocation];

After updating the location, you need to notify the user of the updated location information in some way : agent , block, notification .

}

callback method when location is updated

-(void) Locationmanager: (cllocationmanager *) Manager didupdatelocations: (nsarray< Cllocation *> *) Locations

{

NSLog(@ " location updated ");

// stop positioning

[Manager Stopupdatinglocation];

}

Failed to locate

-(void) Locationmanager: (cllocationmanager *) Manager didfailwitherror: (nserror *) error

{

NSLog(@ " failed to locate ");

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

//Dispose of any resources, can be recreated.

}

@end

The above is my implementation of access to the location, say the following plist file

This is my final plist file, inside the front and rear desk, the foreground, using the location request is added yourself; the key to using the location request is:

Nslocationusagedescription (in the 187th line of the CLLocationManager.h file), as for the front of the privacy is the system himself added, do not have to dwell on this;

In addition, to achieve both front and rear can use positioning, but also to the project targets inside, select (second) capabilities, in capabilities and then select Background Modes, the switch opens, and in which the second location Check in front of updates;

This way you will be able to run your project perfectly.

Corelocation Framework App Locator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.