让位置服务在 IOS 8 中工作

Getting Location Services to work in IOS 8

我正在尝试更新一些旧代码以使其在 IOS 8 中工作。我已通读

Location Services not working in iOS 8

但我仍然对如何正确实施该方法感到困惑。

我在

中添加了
<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>

进入infoPlist.strings

但我不确定如何更新我的代码以使其正常执行。这是我正在使用的旧代码的副本。谁能提供有关如何正确更新此代码以使其与 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;
}

}

我在这里使用的代码来自 parse 的 Anywall 教程的旧版本。我试过添加

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

    }
        break;

此外,该应用程序还会在地图上显示用户的位置,我是否也必须更新该方法?

你必须这样做:

1)#import <CoreLocation/CoreLocation.h>

2)设置委托CLLocationManagerDelegate

3)创建CLLocationManager *locationManager;对象

4)#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) 在您的 .m 文件中

5)设置位置管理器

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) 为 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];
        }
    }
}