2011-12-12 50 views
0

我想在我的应用程序中搜索当前位置,但我发现整个位置服务都是不允许的。当位置服务不允许时,我可以访问核心位置api吗?

@interface LoginViewController : UIViewController <CLLocationManagerDelegate> 
{ 
    CLLocationManager* locationManager; 
    CLLocation* currentLocation; 
} //header file 

//implementation header file 
(void)loginButtonPressed:(id)aSender 
{ 
    if (locationManager == nil) 
    { 
     locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate = self; 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     locationManager.distanceFilter = kCLDistanceFilterNone; 
    }    

    [locationManager startUpdatingLocation]; 
} 

//implementation CLLocationManagerDelegate 
(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"update location"); 
    if(self.currentLocation == nil) 
    { 
     self.currentLocation = newLocation; 
     [locationManager stopUpdatingLocation]; 
    } 
} 

(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"fail with error"); 
    [locationManager stopUpdatingLocation]; 
    self.currentLocation = nil; 
} 

locationManager电话startUpdatingLocation(假设位置服务是不允许的),locationManager不能获得当前位置,并调用didFailWithError: method
在我的情况下,locationManager与iOS5没有任何联系,但拨打didFailWithError: method与iOS4。我不知道我的过错是什么。

回答

0

不,您应该始终检查在启动任何位置更新之前是否允许位置监视。如果用户更改了您的应用的偏好设置,则只会收到错误信息,而不会更新实际的位置信息。

您应该检查位置更新是否可用,如果不是,请通知用户他们需要为您的应用重新启用位置服务才能正常工作。

+0

谢谢你的建议。在locationManager开始调用startUpdatingLocation方法之前,它必须检查CLLocationManager的状态。因此,条件如果语句更改为这样[CLLocationManager locationServicesEnabled] – zeide

相关问题