IOS Cllocationmanager Targeting

Source: Internet
Author: User

In previous versions of IOS8, we had no problem using cllocationmanager positioning, and recently in the IOS8 system it was not possible to locate .... This is a big problem!

1, first define a global variable to record Cllocationmanager object, introduce corelocation.framework use #import <CoreLocation/CoreLocation.h>
@property (nonatomic, strong) Cllocationmanager  *locationmanager;
2. Initialize the Cllocationmanager and start positioning
Locationmanager=[[cllocationmanager alloc] init];    locationmanager.delegate=self;    Locationmanager.desiredaccuracy=kcllocationaccuracybest;    locationmanager.distancefilter=10;     [Locationmanager startupdatinglocation];//open position
3, the implementation of Cllocationmanagerdelegate agent method
#pragma Mark Cllocationmanagerdelegate
/**
* Get latitude and longitude
*/-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{ cllocation * Currlocation=[locations Lastobject]; Location.strlatitude=[nsstring stringwithformat:@ "%f", currLocation.coordinate.latitude]; Location.strlongitude=[nsstring stringwithformat:@ "%f", currLocation.coordinate.longitude]; NSLog (@ "La---%f, lo---%f", currlocation.coordinate.latitude,currlocation.coordinate.longitude);} /** * location failed, callback this method */-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error{ if ([ Error code]==kclerrordenied) { NSLog (@ "Access Denied"); } if ([Error Code]==kclerrorlocationunknown) { NSLog (@ "Cannot get location information");} }

Using corelocation positioning in iOS8

1, before using corelocation need to call the following function "IOS8 dedicated":
IOS8 has made some modifications to the positioning, including the method of locating authorization, Cllocationmanager adds the following two methods:
(1) Always allow access to location information
-(void) requestalwaysauthorization;
(2) Allow access to location data during use of the application
-(void) requestwheninuseauthorization;
   Examples are as follows:
Locationmanager=[[cllocationmanager alloc] init];    locationmanager.delegate=self;    Locationmanager.desiredaccuracy=kcllocationaccuracybest;    locationmanager.distancefilter=10;    if (iosversion>=8) {        [Locationmanager requestwheninuseauthorization];//allows access to location data in the course of the program (IOS8 location required)    }    [Locationmanager startupdatinglocation];//open position
2. Add the following configuration in the Info.plist file:
(1) nslocationalwaysusagedescription
(2) Nslocationwheninuseusagedescription

After this is added, the positioning function will be able to use the normal!

IOS Cllocationmanager Targeting

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.