检查是否接受NSLocationWhenInUseUsageDescription

Abd*_*ami 2 ios ibeacon ios8

我正在开发一个处理信标和位置管理的iOS应用程序.在iOS 8中,应用程序必须在plist文件中包含"NSLocationWhenInUseUsageDescription"键,该文件将在启动时显示在应用程序中,以获得用户启用位置跟踪的权限.如何检测用户是否接受?我想就此作出决定.

谢谢.

Gle*_*n T 5

您可以通过在类设置中实现以下方法作为CLLocationManager委托来检测这一点(实现CLLocationManagerDelegate协议)

Swift中的示例:

// MARK: CLLocationManagerDelegate

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    // check status to see if we’re authorized
    let authorized = (status == CLAuthorizationStatus.AuthorizedWhenInUse)
    // handle acceptance... 
}
Run Code Online (Sandbox Code Playgroud)

目标C示例:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    BOOL isAuthorized = status == kCLAuthorizationStatusAuthorizedWhenInUse;
    // handle acceptance..        
}
Run Code Online (Sandbox Code Playgroud)