Changes in the Core location in iOS 8---Swift

Source: Internet
Author: User

The implementation of the positioning has always been very simple and consistent:

Implementation of OBJECTIVE-C:

#import "ViewController.h" @import corelocation; Introducing Library Files @interface Viewcontroller () <CLLocationManagerDelegate>//Compliance protocol @property (strong, Nonatomic) Cllocationmanager *locationmanager; Define location manager@end@implementation viewcontroller-(void) viewdidload {    [super viewdidload];    Initialize the manager and start positioning    Self.locationmanager = [[Cllocationmanager alloc] init];    Self.locationManager.delegate = self;    [Self.locationmanager startupdatinglocation];} Delegate protocol Method-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{    NSLog (@ "%@", [locations Lastobject]);} -(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error{    NSLog (@ "error =%@", [ Error description]);} @end

Swift's implementation:
Import Uikitimport Corelocationclass Viewcontroller:uiviewcontroller, cllocationmanagerdelegate {let        Locationmanager:cllocationmanager = Cllocationmanager ()        override func Viewdidload () {        super.viewdidload ()        locationmanager.delegate = self        locationmanager.startupdatinglocation ();    }        Func Locationmanager (manager:cllocationmanager!, didupdatelocations locations: [anyobject]!) {        println ("New location = \ (locations.last)}"    }        func Locationmanager (manager:cllocationmanager!, Didfailwitherror error:nserror!) {        println ("error = \ (error.description)    }}}"
However, in the case of iOS8, any proxy method is silent---can neither get the cause or the warning of the failure, nor get the location information. The app also does not have permission to request permission to use location information.It's very harmonious.
There are two things that need to be done in a iOS8 system to make the location effective:

1. Add a pair of keywords in info.plist to license location services. Nslocationalwaysusagedescription and Nslocationwheninuseusagedescription, Two keywords corresponding to the string can be arbitrary, generally as long as the addition of a keyword can be, because there is alwaysusage so wheninuse can be positioned, the reverse is not applicable.

2. Request permission in the code.

Implementation of OBJECTIVE-C:

    if ([Self.locationmanager respondstoselector: @selector (requestwheninuseauthorization)]) {        [ Self.locationmanager requestwheninuseauthorization];    }    or    if ([Self.locationmanager respondstoselector: @selector (requestalwaysauthorization)]) {        [ Self.locationmanager requestalwaysauthorization];    }
Swift's implementation:

        IOS8 method, otherwise it cannot be located        if Locationmanager.respondstoselector (Selector ("Requestwheninuseauthorization")) {            Locationmanager.requestwheninuseauthorization ()        }        //or        if Locationmanager.respondstoselector ( Selector ("Requestalwaysauthorization")) {            locationmanager.requestalwaysauthorization ()        }

Change the authentication status of a geographic location

If you set the Wheninuse at the beginning and then need to use always, you need to change the state in plist and code.

Implementation of OBJECTIVE-C:

if ([Self.locationmanager respondstoselector: @selector (requestalwaysauthorization)    ]) {[Self requestalwaysauthorization]; }
-(void) requestalwaysauthorization{clauthorizationstatus status = [Cllocationmanager authorizationstatus]; If the status is not approved, a popup alert if (status = = Kclauthorizationstatusauthorizedwheninuse | | status = = Kclauthorizationstatusdenie        d) {NSString *title; title = (Status = = kclauthorizationstatusdenied)?        @ "Location is off": @ "background positioning is not turned on";        NSString *message = @ "need to open alway location service";                                                            Uialertview *alertview = [[Uialertview alloc] Initwithtitle:title                                                  Message:message delegate:self        cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "Settings", nil];    [Alertview show];    } else if (status = = kclauthorizationstatusnotdetermined) {[Self.locationmanager requestalwaysauthorization]; }}-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) Buttonindex{if (Buttonindex = = 1) {//Skip to Settings interface Nsurl *settingsurl = [Nsurl urlwithstring:uiapplicationopenset        Tingsurlstring];    [[UIApplication sharedapplication] openurl:settingsurl]; }}
Swift's implementation:
        If Locationmanager.respondstoselector (Selector ("Requestalwaysauthorization")) {            requestalwaysauthorization ()        }
    Func requestalwaysauthorization () {let        status = Cllocationmanager.authorizationstatus ()        if status = =. Denied | | Status = =. Authorizedwheninuse {let            title = (Status = =. Denied)? "Location is Off": "Background positioning does not turn on" let            message = "Location Services requiring alway"            uialertview (Title:title, Message:message, delegate:self , Cancelbuttontitle: "Cancel", Otherbuttontitles: "Set")        } else {            locationmanager.requestalwaysauthorization ()        }    }        Func Alertview (Alertview:uialertview, Clickedbuttonatindex buttonindex:int) {        if Buttonindex = = 1 {            Uiapplication.sharedapplication (). OpenURL (Nsurl (string:uiapplicationopensettingsurlstring)!)        }    }

Changes in the Core location in iOS 8---Swift

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.