这是一款位置应用。当我第一次在iPhone上运行应用程序时,会出现一个警告窗口,询问我是否要允许位置服务。选择“确定”或“不允许”后,我可以点击设置为显示当前位置的按钮。该按钮还设置为使用名为“locationServicesEnabled”的BOOL函数的值将NSLog打印到控制台,如果它们是,则返回yes / 1,如果不是,则返回no / 0。
我的问题是,当我选择“不允许”选项时,按下按钮,NSLog仍会将值1打印到控制台,这是不正确的。它应该是0或false。连接到我的按钮的文本标签甚至显示“您的位置为空”,但由于某种原因,NSLog始终显示BOOL值为1.
这是我的 ViewController.m 代码:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.gpsLM = [[CLLocationManager alloc]init];
[self.gpsLM startUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
}
-(IBAction)gpsButton{
CLLocation * currentLocation = self.gpsLM.location;
self.gpsLabel.text = [NSString stringWithFormat:@"Your Location is %@", currentLocation];
NSLog(@"Location services enabled: %hhd",[CLLocationManager locationServicesEnabled]);
}
@end
答案 0 :(得分:1)
您需要检查[CLLocationManager authorizationStatus]
,而不是locationServicesEnabled
。授权状态返回特定于应用程序的值,具体如下:
typedef enum {
kCLAuthorizationStatusNotDetermined = 0,
kCLAuthorizationStatusRestricted,
kCLAuthorizationStatusDenied,
kCLAuthorizationStatusAuthorized
} CLAuthorizationStatus;
希望这些名字不言自明。
答案 1 :(得分:0)
我刚刚找到了自己问题的答案。如果完全禁用iPhone的位置服务选项,则bool函数将仅返回false值。如果您只有1个应用程序被禁用,但启用了主要功能并且其他应用程序正在使用它,那么它仍将返回值1.看起来,locationServicesEnabled方法是特定于设备的,而不是特定于应用程序。