OGeek|极客世界-中国程序员成长平台

标题: ios - startUpdatingLocation 不再在 XCode 6.1 中工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:51
标题: ios - startUpdatingLocation 不再在 XCode 6.1 中工作

我什至无法让最简单的位置更新代码工作。我有一个位置应用程序运行良好一年多,现在当我尝试编译和运行它(升级到 xcode 6.1 并且没有更改代码之后),在调用 startUpdatingLocation 之后,didUpdateLocations 回调永远不会触发,我可以看到 gps 指示器从未出现在电池指示器旁边。 我已经开始了一个新的测试项目,除了尝试注册位置更新之外什么都不做,但结果仍然相同:设备或模拟器上没有位置更新。 下面是测试项目的单 View Controller 的代码:

//ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{

}

@property (strong, nonatomic) CLLocationManager* locationManager;

@end


//ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [self.locationManager requestWhenInUseAuthorization];

    [self.locationManager startUpdatingLocation];
}

- (void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)oldLocation
{
    NSLog(@"got a location");
}

-(void)locationManagerCLLocationManager *)manager didFailWithErrorNSError *)error
{
    NSLog(@"Error: %@",error.description);
}
-(void)locationManagerCLLocationManager *)manager didUpdateLocationsNSArray *)locations
{
     NSLog(@"got a location");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

如果拥有最新版本的 xcode 的人可以创建一个这样的项目,该项目除了接收位置更新之外什么都不做,验证它是否有效,然后完整地发布代码,那就太棒了。我已尝试将内容添加到 plist 文件以及类似问题的所有其他补救措施均无济于事。



Best Answer-推荐答案


是的,新版本中发生了一些变化,这可能会让人头疼。我遇到了同样的问题,这是为我解决的线程here .

你需要添加这些行 info.plist:

<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>

并确保在尝试更新用户位置之前调用 [CLLocationManager requestWhenInUseAuthorization][CLLocationManager requestAlwaysAuthorization]

关于ios - startUpdatingLocation 不再在 XCode 6.1 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26577645/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4