iOS应用程序位置权限当前不起作用

时间:2016-08-23 16:26:41

标签: ios swift mapkit

这是我的一般ViewController类,我研究了这个问题iOS app doesn't ask for location permission,我已经NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

@IBOutlet weak var ownMapView: MKMapView!

let locationManager : CLLocationManager = CLLocationManager();


override func viewDidLoad() {
    super.viewDidLoad();

    self.locationManager.delegate = self;
    self.locationManager.requestWhenInUseAuthorization();
    self.locationManager.startUpdatingLocation();
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    guard status == .AuthorizedWhenInUse else
    {
        print("Location not using");
        return;
    }

    print("Location using.");
    ownMapView.showsUserLocation = true;
    }

即使我添加NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription,这对我也不起作用。它仅打印"位置不使用"

我如何正确请求?

2 个答案:

答案 0 :(得分:0)

enter image description here让我们添加了NSLocationAlwaysUsageDescription&在plist中的NSLocationWhenInUseUsageDescription? 。添加这些,它将起作用。

答案 1 :(得分:0)

要正确理解这一点,请检查CLLAuthorizationStatus doc: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/#//apple_ref/c/tdef/CLAuthorizationStatus

CLAuthorizationStatus有几个可能的值:

  • 未确定 - 用户尚未决定允许/拒绝使用位置
  • 受限制 - 应用仅限使用位置
  • 拒绝 - 用户拒绝使用位置。
  • AuthorizedAlways - 此应用程序有权随时启动位置服务。
  • AuthorizedWhenInUse - 授权应用程序在前台运行时启动大多数位置服务

您正在检查"授权何时使用"状态,但您首先启动其状态为NotDetermined。

您可以进一步检查以下状态:

.jumbotron {
    background-image:url('http://4.bp.blogspot.com/-CIy9X0Vwlko/VFvyStJbP_I/AAAAAAAACys/Ze7QVlcVmX0/s1600/HOPE%2BLogo%2Bcopy.jpg');
    height: 600px;
    background-repeat: no-repeat;
    background-size: contain;
    background-color: #FFFFFF;
    position: relative;
}
.jumbotron .container {
    position: relative;
    top:100px;
}
.jumbotron h1 {
      color: #fff;
      font-size: 60px;  
      font-family: 'Love Ya Like A Sister', cursive;
      font-weight: bold;
}
.jumbotron p {
    font-size: 30px;
    color: #A80000;
}