簡體   English   中英

當NSLog使用BOOL方法應該返回false時,它返回true

[英]BOOL method is returning true when used by NSLog when it should be returning false

這是一個位置應用。 當我第一次在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

您需要檢查[CLLocationManager authorizationStatus] ,而不是locationServicesEnabled 授權狀態返回特定於應用程序的值,其中之一:

typedef enum {
   kCLAuthorizationStatusNotDetermined = 0,
   kCLAuthorizationStatusRestricted,
   kCLAuthorizationStatusDenied,
   kCLAuthorizationStatusAuthorized
} CLAuthorizationStatus;

希望這些名字不言自明。

我只是想出了自己的問題的答案。 如果完全禁用了iPhone的定位服務選項,則bool函數將僅返回錯誤值。 如果您僅禁用了1個單個應用程序,但是啟用了主要功能並正在使用其他應用程序,則它仍將返回值1。看起來locationServicesEnabled方法是特定於設備的,而不是特定於應用程序的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM