繁体   English   中英

使定位服务在IOS 8中工作

[英]Getting Location Services to work in IOS 8

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

定位服务在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)在.m文件中#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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