iOS应用不会要求位置许可

Una*_*dra 9 core-location mapkit ios swift

我的Swift-iOS应用程序旨在显示用户在地图上的位置.但是,XCode调试控制台告诉我,我需要请求权限才能显示用户的位置.我想,我这样做,但对话永远不会出现.

这是错误消息,在我调用的ViewController下面 CLLocationManager::requestWhenInUseAuthorization()

错误:

2014-06-30 21:25:13.927 RowingTracker2 [17642:1608253]尝试在不提示位置授权的情况下启动MapKit位置更新.必须首先调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization].

视图控制器:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate {
    @IBOutlet var mapview: MKMapView = nil
    var locationmgr : CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        locationmgr = CLLocationManager()
        locationmgr.requestWhenInUseAuthorization()
        mapview.showsUserLocation = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Run Code Online (Sandbox Code Playgroud)

如何申请授权使用该位置?你可以在这里找到完整的项目.(提交)

信息

甚至使视图控制器继承CLLocationManagerDelegate并设置委托self指示这里没有帮助.

Dav*_*rry 26

从iOS 8开始,您必须调用其中一个请求...函数,并将相应的条目添加到您的Info.plist文件中,NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription.

有关更多信息,请参阅此处的参考

更新

确保这一点

  1. 地图以模拟位置为中心.
  2. 还要确保模拟某个位置.可以在XCode的调试区域(下方)中进行(参见图像),也可以在模拟器下进行Debug > Location.

调试区域: 调试区域中的位置模拟


Cay*_*nte 8

您需要使用requestWhenInUseAuthorization并且还需要在yourapp-Info.plist上创建一个名为NSLocationWhenInUseUsageDescription的值


归档时间:

查看次数:

17860 次

最近记录:

6 年,10 月 前