提醒iOS 9中的位置服务权限

时间:2016-03-08 11:13:12

标签: ios xcode swift cllocationmanager

我已将NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription设置为info.plist,当应用想要使用位置服务时会出现提醒,但警报只会显示一段时间,我可以& #39; t点击此提醒。此警报会自动消失!

 func showLocation(sender: UIButton)
    {
        let manager = CLLocationManager()
        manager.delegate = self
        if CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedAlways
        {
            manager.requestWhenInUseAuthorization()
        }
        manager.startUpdatingLocation()
    }
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        for location in locations {
            print("The location is (location)")
        }
    }

1 个答案:

答案 0 :(得分:2)

self.locationManager = [[CLLocationManager alloc]init];
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
    if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] && [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
        [self.locationManager requestWhenInUseAuthorization];
    }
}
else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
    //Location Services is off from settings

}
else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted)
{

}

尝试以下代码。它对我有用