iOS 应用程序不要求位置权限

标签 ios swift mapkit core-location

我的 Swift-iOS 应用旨在显示用户在 map 上的位置。但是,XCode 调试控制台告诉我需要请求权限才能显示用户的位置。我想,我这样做了,但对话从未出现。

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

的 ViewController 下方

错误:

2014-06-30 21:25:13.927 RowingTracker2[17642:1608253] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

View Controller :

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.
    }
}

如何请求授权使用该位置? 您可以找到完整的项目 here .( Commit )

信息

甚至使 ViewController 继承自 CLLocationManagerDelegate 并将委托(delegate)设置为 self,如 here 所示没有帮助。

最佳答案

从 iOS 8 开始,您必须调用其中一个请求...函数并将适当的条目添加到您的 Info.plist 文件中,NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

有关详细信息,请参阅引用资料 here

更新

确保

  1. map 以模拟位置为中心。
  2. 还要确保位置是模拟的。在 XCode(见图片)的调试区(下方)中执行此操作,或在 Debug > Location 下的模拟器中执行此操作。

调试区: Location Simulation in the debug area

关于iOS 应用程序不要求位置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24497928/

相关文章:

ios - 通过 Cocoapods 安装 RestKit 0.20.0 后基础类型未知

ios - iOS中的Applozic用户列表创建和删除

json - 类型仅在运行时已知时的 Swift 4 JSON 解码

swift - AVFoundation:使用 AVAudioEngine 和 AVAudioFile 的格式错误的 .m4a 文件格式

iPhone谷歌地图获取地址的纬度和经度

ios - 我的 CollectionViewCell 陷入困惑

iOS设置单元格高度取决于url图像高宽比

ios - 将 UITouch 管理从 TouchBegan 切换到 UIScrollViewDelegate

ios - 固定颜色未按预期正常显示

iphone - 对于 MapKit,我应该使用 Directions 还是制作我自己的路线叠加层?