简体   繁体   中英

Getting Location Services to work in IOS 8

I am trying to update some old code to get it to work in IOS 8. I have read through

Location Services not working in iOS 8

but I am still very confused as to how to correctly implement the methodology.

I have added in

<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>

into the infoPlist.strings

but I am not sure how to update my code to get it to execute properly. Here is a copy of the old code that I am using. Can anyone provide insight on how to properly update this code in order to get it to work with IOS 8?

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSLog(@"%s", __PRETTY_FUNCTION__);

switch (status) {
    case kCLAuthorizationStatusAuthorizedAlways:
    {
        NSLog(@"kCLAuthorizationStatusAuthorized");
        // Re-enable the post button if it was disabled before.
        self.navigationItem.rightBarButtonItem.enabled = YES;
        [self.locationManager startUpdatingLocation];
    }
        break;
    case kCLAuthorizationStatusDenied:
        NSLog(@"kCLAuthorizationStatusDenied");
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Anywall can’t access your current location.\n\nTo view nearby posts or create a post at your current location, turn on access for Anywall to your location in the Settings app under Location Services." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertView show];
        // Disable the post button.
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
        break;
    case kCLAuthorizationStatusNotDetermined:
    {
        NSLog(@"kCLAuthorizationStatusNotDetermined");


    }
        break;
    case kCLAuthorizationStatusRestricted:
    {
        NSLog(@"kCLAuthorizationStatusRestricted");
    }
        break;
}

}

The code I am using here is from an old version of parse's Anywall Tutorial. I have tried adding in

case kCLAuthorizationStatusNotDetermined:
    {
        NSLog(@"kCLAuthorizationStatusNotDetermined");
        [self.locationManager requestAlwaysAuthorization];

    }
        break;

In addition the application also displays the user's location on a map do I have to update that method as well?

You have to do it like this:

1) #import <CoreLocation/CoreLocation.h>

2)Set delegate CLLocationManagerDelegate

3)Create CLLocationManager *locationManager; Object

4) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) in your .m file

5)Setup location manager

if (self.locationManager==nil) {
    self.locationManager = [[CLLocationManager alloc]init];
}
self.locationManager.delegate = self;
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        NSString *title;
        title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
        NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Settings", nil];
        [alertView setTag:1001];
        [alertView show];
        [alertView release];
    }
    else{
        NSString *titles;
        titles = @"Title";
        NSString *msg = @"Location services are off. To use location services you must turn on 'Always' in the Location Services Settings from Click on 'Settings' > 'Privacy' > 'Location Services'. Enable the 'Location Services' ('ON')";
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:titles
                                                            message:msg
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];
        [alertView release];

    }


}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        [self.locationManager requestAlwaysAuthorization];

    }

}

[self.locationManager startUpdatingLocation];

6)Setup alert view for iOS 8

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (alertView.tag==1001){
        if (buttonIndex == 1) {
            // Send the user to the Settings for this app
            NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:settingsURL];
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM