我已将NSLocationAlwaysUsageDescription
和NSLocationWhenInUseUsageDescription
设置为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)")
}
}
答案 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)
{
}
尝试以下代码。它对我有用