【问题标题】:startUpdatingLocation no longer working in XCode 6.1startUpdatingLocation 不再在 XCode 6.1 中工作
【发布时间】:2014-10-26 20:35:39
【问题描述】:

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

//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)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"got a location");
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
     NSLog(@"got a location");
}

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

@end

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

【问题讨论】:

  • 当我将 NSLocationWhenInUseUsageDescription 添加到 info.plist 时,对我来说效果很好。您的代码已复制并粘贴到新的单一视图应用程序中。

标签: ios cllocationmanager


【解决方案1】:

是的,新版本中发生了一些变化,这可能会让人头疼。我遇到了同样的问题,这是为我解决它的线程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]

【讨论】:

  • 我之前曾尝试添加所有这些,但是当我将您的 plist 行直接复制到 plist 源中时,它起作用了!谢谢你的神奇组合!我在精神错乱中摇摇欲坠。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多