2014-10-06 52 views
6

花了几天试图解决这个问题,但无法找到任何有效的解决方案。我已经检查了所有在stackoverflow的帖子,并尝试了所有的解决方案,似乎没有任何工作。我也尝试了CLLocation的苹果测试项目,对我来说工作正常。我以前的点点滴滴,从苹果的测试项目CLLocation didupdatetolocation不叫

https://developer.apple.com/library/ios/samplecode/LocateMe/Listings/README_md.html

但我的代码是不工作的。 DidupdateToLocation永远不会被调用。

这里是我的代码(在viewDidLoad中)

_locationManager = [[CLLocationManager alloc] init]; 

self.locationManager.delegate = self; 



// This is the most important property to set for the manager. It ultimately determines how the manager will 

// attempt to acquire location and thus, the amount of power that will be consumed. 

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 



// Once configured, the location manager must be "started" 

// 

// for iOS 8, specific user level permission is required, 

// "when-in-use" authorization grants access to the user's location 

// 

// important: be sure to include NSLocationWhenInUseUsageDescription along with its 

// explanation string in your Info.plist or startUpdatingLocation will not work. 

// 

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 

    [self.locationManager requestWhenInUseAuthorization]; 

} 

[self.locationManager startUpdatingLocation]; 



[self performSelector:@selector(stopUpdatingLocationWithMessage:) 

      withObject:@"Timed Out" 

      afterDelay:30]; 

我有检查,以确保locationServicesEnabled启用。

我已将NSLoationWhenInUseUsageDescription属性添加到info.plist文件。我需要添加或启用任何服务还有其他任何属性吗?

我不能为上帝的爱搞清楚我做错了什么。有人可以帮我解决这个问题。

+0

btw会弹出授权警报? – 2014-10-06 16:34:58

+0

授权警报不会弹出 – dogwasstar 2014-10-07 00:05:43

+1

然后我猜didChangeAuthorizationStatus回调既不会被调用,对吧?该应用的位置服务已启用? (设置/隐私/位置服务)您确定您向.plist添加了正确的密钥吗? – 2014-10-07 07:14:08

回答

0

傻我。我将关键字添加到错误的plist文件中。我现在已经开始工作了。谢谢你们的帮助。

6

您必须在收到iOS8上的didChangeAuthorizationStatus回调后调用startUpdatingLocation。

//iOS 8 API change 
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){ 
    [self.locationManager requestWhenInUseAuthorization]; 
}else{ 
    [self.locationManager startUpdatingLocation]; 
} 

实现这个委托方法:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 
    switch (status) { 
     case kCLAuthorizationStatusNotDetermined: 
     case kCLAuthorizationStatusRestricted: 
     case kCLAuthorizationStatusDenied: 
     { 
      // do some error handling 
     } 
      break; 
     default:{ 
      [self.locationManager startUpdatingLocation]; 
     } 
      break; 
    } 
} 
6

首先,不要忘了,与IOS 8,你需要做的[locationManager requestWhenInUseAuthorization][locationManager requestAlwaysAuthorization];

_locationManager = [CLLocationManager new]; 

if(SYSTEM_IS_OS_8_OR_LATER) { 
    [_locationManager requestWhenInUseAuthorization]; 
    [_locationManager requestAlwaysAuthorization]; 

} 

_locationManager.delegate = self; 
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
//and the problem with your code : don't forget to start 
_locationManager startUpdatingLocation]; 

,并在您didUpdateLocations

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

     _currentLocation = [locations objectAtIndex:0]; 
     //do your stuff with the location 

    } 
+0

美妙。添加'requestWhenInUseAuthorization'和'requestAlwaysAuthorization'是解决方案。 – 2014-10-08 05:19:46

0

我只是整个早上花这个[坦率地我是相对较新OOC],反正这个问题..

didUpdateLocation编译并不会被调用 didUpdateLocations编译并没有工作!

不是在这种情况下的答案,它似乎,但它是在这个问题上,一个容易犯的错误。使用IOS 8.1.3 & Xcode 6.1.1

0

只需在Info.plist中添加三个属性即可。

要获取当前位置,如果它不叫didUpdateToLocation方法

String类型的NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription privacy - location usage description