2012-10-04 51 views
3

我制作位置应用程序。但核心位置不起作用。 我在iPhone上测试了其他iPhone应用程序。
像谷歌地球,一个导航软件。 其他应用程序也不起作用。
为什么不更新位置?
为什么'locationManager:didUpdateToLocation:fromLocation:'消息只调用了2次?
iOS 6 CoreLocation不起作用

也许...我的iPhone坏了?或iOS 6 CoreLocation框架有一些错误?

位置服务 - 在iPhone上设置

的Info.plist

  • 的ARMv7
  • 加速度计
  • 位置服务
  • GPS
  • 麦克风
  • 磁力

代码示例:

- (CLLocationManager *)setupLocationManager 
{ 
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager headingAvailable]) { 

    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.headingFilter = kCLHeadingFilterNone; 
    [locationManager startUpdatingLocation]; 
    [locationManager startUpdatingHeading]; 
    return locationManager; 
    } 
    return nil; 
} 

- (CLLocationManager *)locationManager 
{ 
    switch([CLLocationManager authorizationStatus]) 
    { 
    case kCLAuthorizationStatusAuthorized: 
     _deltaTimeLocationReceived = 0.0; 
     if (_locationManager == nil) 
     _locationManager = [self setupLocationManager]; 
     return _locationManager; 

     case kCLAuthorizationStatusDenied: 
     case kCLAuthorizationStatusRestricted: 
     if (_locationManager) 
      _locationManager = nil; 
     return _locationManager; 

     case kCLAuthorizationStatusNotDetermined: 
     _deltaTimeLocationReceived = 0.0; 
     if (_locationManager == nil) 
      _locationManager = [self setupLocationManager]; 
     return nil; 
    } 
    return nil; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"%@ %@", NSStringFromSelector(_cmd), newLocation.description); 
    if (self.locationManager) _locationSignal++; 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"%@ %@", NSStringFromSelector(_cmd), error.description); 
} 
+0

你清除了你的安全设置吗? – Jessedc

+0

是的,我设置了位置服务 - 开。谢谢。 – booiljoung

+0

设置 - >常规 - >重置 - >重置位置和隐私。 – Jessedc

回答

0

你有没有从委托方法的任何消息?

如果没有消息,请检查您的类的接口说明。

@interface ... < ... CLLocationManagerDelegate ...>

+0

是的,我添加了委托。谢谢。 – booiljoung

8

在iOS 6中苹果已经通过实施AutoPause API重大更改到核心位置。 AutoPause API会在应用程序进入后台时暂停位置更新并适用于一些条件(即用户不移动,无位置修复,用户停止活动)。为了准确处理Apple提出的暂停事件请求,以帮助更好地预测是否暂停位置更新,以设置活动类型(即导航,健身等)。 默认情况下,在针对iOS 6编译应用程序时,启用AutoPause API。

简单的解决方法是通过始终将'pausesLocationUpdatesAutomatically'设置为NO来禁用AutoPause API。位置更新将在应用云在后台甚至派,喜欢它用在<的iOS 6

更多细节在这里工作:

http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

+0

谢谢andreapavan。 – booiljoung

+0

我添加了代码'pausesLocationUpdatesAutomatically = NO'。我有需要的。 – booiljoung

+0

但你见过自动暂停回调被调用吗? – Naresh

3

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation委托犯规存在于iOS 6的了。

而是使用

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

检查Apple documentation for startUpdateLocation

此代码(以及pausesLocationUpdatesAutomatically)将只编译XCode中4.5(其中基SDK是6)。

如果你想前6.0瞄准的iOS版本,然后用这个宏

#define IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) 

何地,你所创建的CLLocationManager对象

if(IOS_VERSION_GREATER_THAN_OR_EQUAL_TO (@"6.0")) 
{ 
    locationManagerObj.pausesLocationUpdatesAutomatically = NO; 
} 

的的LocationManager代表应在编译时在所有版本中运行来自Xcode 4.5

+0

*我支持的所有版本 –