Adaptation ios8.0/7.0 positioning

Source: Internet
Author: User

phenomenaIf you used the iOS8 to run on ios8.0 's machine, would you find it? How to Locate the function can not be used , the upper right corner of the positioning of the small icon does not come out. At this point you should go to the settings inside to see the privacy-positioning, see not open, and then change to always, then the program ran again, and then you found that still can not use ... So you go to set up there to see, by the discovery of the change to always how to cancel. (Actually this is me.) )
causeIn Previous versions of iOS, when you started using Location Services, a dialog box was automatically popped up asking for authorization, and now you need to call Locationmanager manually in IOS8 Requestalwaysauthorization Manually request authorization to obtain the location permission. The apples gave us two things after the ios8.0.

-(void) requestwheninuseauthorization __osx_available_starting(__mac_na, __iphone_8_0);

-(void) requestalwaysauthorization __osx_available_starting(__mac_na, __iphone_8_0);

These two things are used to manually get the location permissions. You can separately set the note to see when you use the above first will prompt the window when you use to get your position, below this will pop up when you are not using can, of course the following this more powerful ...
That 's what Apple says, so let's go see the API.
The method in the ios8.0

If you use Baidu positioning or any third-party positioning is OK, because the current method is only to provide a positioning function to open the role, we need to use the Apple's own positioning parameters

        _locationmanager = [[Cllocationmanager alloc] init];//Create location manager        _locationmanager.delegate = (id) self;        _locationmanager.desiredaccuracy=kcllocationaccuracybest;        _locationmanager.distancefilter=100.0f;        Whether the location service is available        _enable=[cllocationmanager locationservicesenabled];        Whether it has location rights        _status=[cllocationmanager Authorizationstatus];        if (!_enable | | _status<3) {            //request Permission            [_locationmanager requestalwaysauthorization];//            [_ Locationmanager requestwheninuseauthorization];        }

Remember Locationmanager must be a global variable , cannot be a local variable, when a local variable is manually positionedhint Alertview will flash past! I have just started to appear on the No, the point is too late, so certainly not, the back to try to try on it, the original is inadvertently set as the global on the line.


add a key to the Info.plist: Nslocationalwaysusagedescription,value seems to be casually set.

or nslocationwheninuseusagedescription BOOL type value is yes or it seems to be arbitrarily set ...


The system will then prompt if it is turned on. Of course, the above method is placed inside the appdelegate, the first time the program started, the first to tell the user.

But if we remind you when we use them , if you write like this, after asking

[_userlocationstartuserlocationservice];

What will happen, this will cause the first show does not come out, because when asked this Baidu's open positioning function has been implemented , so the first time not, you came in the second time to show the correct.

Then we need to use

-(void) Locationmanager: ( Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status;

This is what happens when the state changes, but I find that the first point will be executed once, when your position will be executed again, as to why I did not think too much, in short I was in the judgment

-(void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status{        _enable=[cllocationmanager locationservicesenabled];    _status=[cllocationmanager Authorizationstatus];    if (!_enable | | _status<3) {            }else{        [_mapview viewwillappear];        [_userlocation Startuserlocationservice];    _mapview.showsuserlocation = no;//First turns off the displayed positioning layer        _mapview.usertrackingmode = bmkusertrackingmodenone;// Set positioning status        _mapview.showsuserlocation = yes;//show positioning layer                _mapview.delegate = self;        _geocodesearch.delegate = self;        _userlocation.delegate = self;        _walkingrouteplanoption.delegate = self;    }}
That's it. At this point, place all the places you want to locate. ButWhen you finish this, you'll get an error on the iOS7., because you used the iOS8 method, at this time we are going to fit.

adaptation ios8.0/7.0 positioning

#define Iosversion [[[Uidevice Currentdevice] systemversion] floatvalue]

#define Isios7later! (Iosversion < 7.0)

#define Isios8later! (Iosversion < 8.0)


Paste the code directly

you can write this in appdelegate.

    if (isios8later) {        _locationmanager = [[Cllocationmanager alloc] init];//Create location manager        _locationmanager.delegate = ( ID) self;        _locationmanager.desiredaccuracy=kcllocationaccuracybest;        _locationmanager.distancefilter=100.0f;        Whether the location service is available        _enable=[cllocationmanager locationservicesenabled];        Whether it has location rights        _status=[cllocationmanager Authorizationstatus];        if (!_enable | | _status<3) {            //request Permission            [_locationmanager requestalwaysauthorization];//            [_ Locationmanager requestwheninuseauthorization];        }    }    return YES;

If you were in the controller,

-(void) viewdidload

   if (isios8later) {        _locationmanager = [[Cllocationmanager alloc] init];//Create location manager        _locationmanager.delegate = ( ID) self;        _locationmanager.desiredaccuracy=kcllocationaccuracybest;        _locationmanager.distancefilter=100.0f;        Whether the location service is available        _enable=[cllocationmanager locationservicesenabled];        Whether it has location rights        _status=[cllocationmanager Authorizationstatus];        if (!_enable | | _status<3) {            //request Permission            [_locationmanager requestalwaysauthorization];            [_locationmanager requestwheninuseauthorization];        }    } else{        [_userlocation startuserlocationservice];        _mapview.showsuserlocation = no;//First turns off the displayed positioning layer        _mapview.usertrackingmode = bmkusertrackingmodenone;//Sets the status of the positioning        _mapview.showsuserlocation = yes;//show anchor layer            }

-(void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status{        _enable=[cllocationmanager locationservicesenabled];    _status=[cllocationmanager Authorizationstatus];    if (!_enable | | _status<3) {            }else{        [_mapview viewwillappear];        [_userlocation Startuserlocationservice];    _mapview.showsuserlocation = no;//First turns off the displayed positioning layer        _mapview.usertrackingmode = bmkusertrackingmodenone;// Set positioning status        _mapview.showsuserlocation = yes;//show positioning layer                _mapview.delegate = self;        _geocodesearch.delegate = self;        _userlocation.delegate = self;        _walkingrouteplanoption.delegate = self;    }}

-(void) Viewwillappear: (BOOL) animated{    [Super viewwillappear:animated];    if (isios8later) {        if (!_enable | | _status<3) {            //request Permission            [_locationmanager requestalwaysauthorization];            [_locationmanager requestwheninuseauthorization];        }    } else{        [_mapview viewwillappear];        [_userlocation Startuserlocationservice];        _mapview.showsuserlocation = no;//First turns off the displayed positioning layer        _mapview.usertrackingmode = bmkusertrackingmodenone;//Sets the status of the positioning        _mapview.showsuserlocation = yes;//display positioning layer                _mapview.delegate = self;        _geocodesearch.delegate = self;        _userlocation.delegate = self;        _walkingrouteplanoption.delegate = self;    }}

Of course, you can continue to optimize, the location of the part I just learned iOS time to do, write not very good, not much to say,that's the general idea.。

Adaptation ios8.0/7.0 positioning

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.