【问题标题】:Having problems with iOS8 Core LocationiOS8核心位置有问题
【发布时间】:2015-04-13 08:37:39
【问题描述】:

Core Location 使用情况最近发生了变化,但即使更新到新规范,我似乎也无法正常工作。

我尝试使用仅在 GPS 坐标查找或失败时更改其背景颜色的视图控制器开始一个新项目。

#import "ViewController.h"

@interface ViewController (){

    CLLocationManager *locationManager;

}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if (locationManager == nil)
    {
        locationManager = [[CLLocationManager alloc] init];
    }


    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    if (![CLLocationManager locationServicesEnabled]) {
        [self.view setBackgroundColor:[UIColor redColor]];
    }

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [locationManager requestAlwaysAuthorization];
    [locationManager startUpdatingLocation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark CLLocationManagerDelegate
//
//
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
{
    [self.view setBackgroundColor:[UIColor greenColor]];
    NSLog(@"lat:%f lon:%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    //Debug(@"did fail with error. stop updating and return error.");

    [self.view setBackgroundColor:[UIColor orangeColor]];
}

@end

我加了

<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs your location to provide the best service.</string>

到 Info.plist 文件,但仍然...没有变化,位置图标出现在电池附近的状态栏上,但没有出现任何位置,无论是真实的还是模拟的。

我错过了什么吗?

【问题讨论】:

    标签: ios objective-c ios8 core-location


    【解决方案1】:

    尝试添加变更授权方式:

    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
        // This method has to be here to trigger the UIAlertView
        switch (status) {
            case kCLAuthorizationStatusDenied:
                break;
            case kCLAuthorizationStatusRestricted:
               break;
            case kCLAuthorizationStatusNotDetermined: {
                if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
                    [self.locationManager requestAlwaysAuthorization];
            }
            default:
                break;
        }
    }
    

    编辑

    您使用了错误的委托方法。 didUpdateToLocation 已被弃用。改用这个:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {}
    

    【讨论】:

    • 谢谢,但没有区别 ;(
    • 出现的位置图标表明设备正在监控该位置。你是说委托回调永远不会被触发?
    • 没问题。应该在项目中作为警告出现,但可能不会。 :-) 祝你好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2015-08-28
    相关资源
    最近更新 更多