2014-10-26 78 views
2

即使最简单的位置更新代码工作也无法实现。我有一个位置应用,运行良好一年多了,现在当我尝试编译并运行它(在升级到xcode 6.1并且没有更改代码之后),在调用startUpdatingLocation之后,didUpdateLocations回调从不会触发,而我可以看到gps指示符不会出现在电池指示符旁边。 我已经开始了一个新的测试项目,它除了尝试注册位置更新,并且仍然是相同的结果:在设备或模拟器上没有位置更新。 下面是测试项目的单一视图控制器代码:startUpdatingLocation不再在XCode 6.1中工作

//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文件中添加内容,并尝试将所有其他类似问题的补救措施无效。

+0

当我将NSLocationWhenInUseUsageDescription添加到info.plist时,对我来说工作得很好。您的代码被复制并粘贴到一个新的单一视图应用程序中。 – InsertWittyName 2014-10-26 20:44:26

回答

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]您尝试更新用户的位置之前。

+0

我曾尝试添加所有这些,但是当我将plist行直接复制到plist源代码中时,它就起作用了!谢谢你的魔力组合!我疯狂地摇摇欲坠。 – steven 2014-10-26 21:24:56

+0

真棒,很高兴听到它的工作! – SuperKevin 2014-10-26 21:31:19