我正在尝试获取我的iOS应用程序中的用户位置。我首先在我的项目中加入了协同定位框架。然后单击一个按钮,我将调用核心位置api,如下所示。当我尝试在设备上安装它时,核心位置从不询问用户权限。当我尝试在点击按钮时获取位置时,我得到的是kCLAuthorizationStatusNotDetermined作为authorisationStatus。在这方面请帮帮我。我不知道发生了什么。
- (IBAction)fetchLessAccurateLocation:(id)sender {
[self.txtLocation setText:@""];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 1000;
if ([self shouldFetchUserLocation]) {
[locationManager startUpdatingLocation];
}
}
这是我的shouldFetchUserLocation方法:
-(BOOL)shouldFetchUserLocation{
BOOL shouldFetchLocation= NO;
if ([CLLocationManager locationServicesEnabled]) {
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusAuthorized:
shouldFetchLocation= YES;
break;
case kCLAuthorizationStatusDenied:
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"App level settings has been denied" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}
break;
case kCLAuthorizationStatusNotDetermined:
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The user is yet to provide the permission" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}
break;
case kCLAuthorizationStatusRestricted:
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The app is recstricted from using location services." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}
break;
default:
break;
}
}
else{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The location services seems to be disabled from the settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}
return shouldFetchLocation;
}
下面是我的核心位置委托方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0){
NSLog(@"location fetched in delegate");
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
// If the event is recent, do something with it.
NSLog(@"inside loop.... latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
}
NSLog(@"latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
[self.txtLocation setText:[NSString stringWithFormat:@"\nlatitude: %+.6f \nlongitude: %+.6f", location.coordinate.latitude, location.coordinate.longitude]];
[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];
if(locationManager!=nil){
locationManager.delegate= nil;
locationManager= nil;
}
}
发布于 2014-09-30 16:27:59
您必须在您的info.plist
中添加NSLocationWhenInUseUsageDescription
。这有点奇怪,但只需将其作为不带文本的字符串值添加,然后当您想要位置时以:
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];
发布于 2014-10-29 15:18:30
在iOS 8中,您需要做两件额外的事情才能使location正常工作:向您的Info.plist添加一个密钥,并请求location管理器授权启动它。有两个用于新位置授权的Info.plist密钥。这些密钥中的一个或两个都是必需的。如果这两个键都不存在,则可以调用startUpdatingLocation,但位置管理器不会实际启动。它也不会向委托发送失败消息(因为它从来没有启动过,所以它不会失败)。如果您添加了一个或两个密钥,但忘记显式地请求授权,它也会失败。
因此,您需要做的第一件事是将以下一个或两个密钥添加到Info.plist文件中:
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
这两个键都有一个字符串,它描述了为什么需要位置服务。您可以输入一个字符串,如“Location is required to find to are”,该字符串可以在InfoPlist.strings文件中进行本地化,就像在iOS 7中一样。
接下来,您需要为相应的位置方法、WhenInUse或Background请求授权。使用以下调用之一:
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
发布于 2013-12-19 02:30:01
卸载该应用程序,然后再次尝试运行它。
如果不起作用,请转到设置并禁用对该应用程序的授权。在那之后,再次运行它,看看它是否询问权限。
或者:
你可以用下面这样的代码强制应用程序开始监控位置:
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
在您的委托方法中,您可以检测是否存在获取位置的错误,并且可以通知用户。
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// Delegate of the location manager, when you have an error
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"application_name", nil) message:NSLocalizedString(@"location_error", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil];
[errorAlert show];
}
如果你有任何问题,请告诉我。
https://stackoverflow.com/questions/20664928
复制相似问题