【问题标题】:Alert for permission of location service in iOS 9iOS 9 中位置服务权限的提示
【发布时间】:2016-06-22 07:22:26
【问题描述】:

我在info.plist中设置了NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription,当应用程序要使用定位服务时会出现警报,但警报只是出现了一点时间,我无法点击此警报.此警报自动消失!

 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)")
        }
    }

【问题讨论】:

  • 一步一步检查,看看你用对了没有。 matthewfecher.com/app-developement/…
  • 在你的方法alertView.dismissWithClickedButtonIndex(0, animated: true)的任何地方都用在这一行中
  • 请提供一些代码
  • 这是一个 CLLocationManager 的小测试演示。当我单击一个按钮时,会调用一个操作。在此操作中,我创建 CLLocationManager 的实例并请求用户许可。应该有许可警报,是的。但是没有我的点击,警报就会自动消失。这是我第一次使用堆栈溢出,我的英语还不错。我不知道我的表达是否正确,对不起。并感谢您的回答!
  • 没有关于警报的代码,当调用requestWhenInUseAuthorization()方法时会出现此警报。然后这个警报很快就消失了@Anbu.Karthik

标签: ios xcode swift cllocationmanager


【解决方案1】:
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)
{

}

试试下面的代码。它对我有用

【讨论】:

  • 你是对的,CLLocationManager 的实例必须是类级别的。并感谢您编写代码的框架。
  • 非常感谢能够帮助你:)
猜你喜欢
  • 2016-09-17
  • 1970-01-01
  • 2015-09-30
  • 2020-09-21
  • 2013-02-15
  • 2016-01-28
  • 2015-02-10
  • 2017-09-20
  • 2018-03-29
相关资源
最近更新 更多