2014-10-20 121 views
0

我在问这个,因为我有点困惑。我没有在我的Info.plist设置NSLocationAlwaysUsageDescription这是我运行代码:总是在不需要的位置

CLLocationManager *locMan = [[CLLocationManager alloc] init]; 
locMan.delegate = self; 
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) 
{ 
    [locMan requestAlwaysAuthorization]; 
} 

[locMan startUpdatingLocation]; 

我设置一个断点,并证实这确实被调用,但我不是在应用程序发出警报。我检查了我的隐私设置,看到该应用程序有两个选项:无和总是,没有检查。我试图卸载,清理并重新安装,但仍然无法正常工作。有任何想法吗?

编辑: 我搬离viewDidLoad上述块为viewDidAppear:,现在警报出现和消失半秒后

回答

1

我解决我的问题的末尾添加这一行。这是一个保留问题。我在本地宣布我的CLLocationManager,所以当它到达我的方法结束时,它不再存在,这就解释了为什么它不能请求许可。我宣传它是一个属性,现在一切正常

+0

地狱,我以为你已经使它成为全球... – pankaj 2014-10-20 14:34:08

0

怎么样,如果你试试这个:

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
    [locationManager requestWhenInUseAuthorization]; 
} 

[locationManager startUpdatingLocation]; 

对不起,missread您帖子。也许试试这个:

CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; 

if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) { 
    // Show alert or something 
} 
else if (status == kCLAuthorizationStatusNotDetermined) { 
    [self.locationManager requestAlwaysAuthorization]; 
} 

更可以在这里读到:Core Location Manager Changes in iOS 8

0

你需要在你的代码

[self.locationManager startUpdatingLocation]; 
+0

我已经添加了该行,并且我得到了相同的结果 – Chris 2014-10-20 14:13:10

+0

尝试在viewWillAppear中移动这段代码。 – pankaj 2014-10-20 14:18:00