简体   繁体   English

iOS应用不会要求位置许可

[英]iOS app doesn't ask for location permission

My Swift-iOS app is meant to show the user's location on a map. 我的Swift-iOS应用程序旨在显示用户在地图上的位置。 However, the XCode debug console tells me I need to ask permission to show the user's location. 但是,XCode调试控制台告诉我,我需要请求权限才能显示用户的位置。 I think, I do that but the dialog never comes up. 我想,我这样做,但对话永远不会出现。

Here is the error message, and below the ViewController where I call CLLocationManager::requestWhenInUseAuthorization() 这是错误消息,在ViewController下面我调用CLLocationManager::requestWhenInUseAuthorization()

Error: 错误:

2014-06-30 21:25:13.927 RowingTracker2[17642:1608253] Trying to start MapKit location updates without prompting for location authorization. 2014-06-30 21:25:13.927 RowingTracker2 [17642:1608253]尝试在不提示位置授权的情况下启动MapKit位置更新。 Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first. 必须首先调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]。

ViewController: 视图控制器:

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

How do I request authorization to use the location? 如何申请授权使用该位置? You can find the complete project here .( Commit ) 你可以在这里找到完整的项目。( 提交

Info 信息

Even making ViewController inherit from CLLocationManagerDelegate and setting the delegate to self as indicated here doesn't help. 甚至使视图控制器继承CLLocationManagerDelegate和委托设置为self所示这里没有帮助。

As of iOS 8 you have to call one of the request... functions and add the appropriate entry to your Info.plist file, either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription . 从iOS 8开始,您必须调用其中一个请求...函数,并将相应的条目添加到Info.plist文件中, NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

For more information, see the reference here 有关更多信息,请参阅此处的参考

Update 更新

Make sure that 确保这一点

  1. the map is centered on the simulated location. 地图以模拟位置为中心。
  2. Also make sure that a location is simulated. 还要确保模拟某个位置。 Either do so in the Debug Area (down below) of XCode (see image), or do it in the simulator under Debug > Location . 可以在XCode的调试区域(下方)中执行此操作(请参见图像),也可以在“ Debug > Location下的模拟器中执行此操作。

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

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

I use NSLocationAlwaysUsageDescription with the value as the text that will pop up when asking for permission, such as 我使用NSLocationAlwaysUsageDescription ,其值为在请求权限时弹出的文本,例如

"I would like permission to spy on you 24/7" “我希望得到许可,可以24/7全天候监视你”

I would also add NSLocationWhenInUseUsageDescription with the value as the message. 我还将添加NSLocationWhenInUseUsageDescription ,并将值作为消息。

As David Berry , Cayke Prudente and Levi Johnson mentioned, I just needed to add NSLocationAlwaysUsageDescription to my Info.plist file. 正如David BerryCayke PrudenteLevi Johnson所提到的,我只需要将NSLocationAlwaysUsageDescription添加到我的Info.plist文件中。 To understand more WHY I needed this, I went for further documentation and I'm sharing here, as it can help others the same way they helped me. 要了解更多我为什么需要这个,我去了进一步的文档,我在这里分享,因为它可以帮助他人,就像他们帮助我一样。

The user prompt contains the text from the NSLocationWhenInUseUsageDescription key in your app's Info.plist file, and the presence of that key is required when calling this method. 用户提示包含应用程序的Info.plist文件中NSLocationWhenInUseUsageDescription键的文本,并且在调用此方法时需要存在该键。

https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有地理位置网站的 Flutter iOS webview 应用程序不会请求位置权限,即使 info.plist 已调整 - Flutter iOS webview app with Geolocation website doesn't Ask For Location Permission even info.plist is adjusted 应用程序不询问位置服务许可whenInUse在安装时? - App doesn't ask location service permission WhenInUse when installation? MapView不会要求位置权限 - MapView doesn't ask for location permission Mac上的iOS 7模拟器不适用于自定义位置(也不会征得许可) - iOS 7 Simulator on Mac doesn't work with custom location (Also doesn't ask permission) 在ioS中,它不询问邮件应用程序的选项 - In ioS, it doesn't ask options of mail app ios7没有收到提示询问用户权限的ios7 registerForRemoteNotificationTypes - ios7 registerForRemoteNotificationTypes doesn't get prompted to ask the user for permission 如果托管iOS应用没有位置权限,WebView页面能否获取位置? - Can a WebView page get location if the hosting iOS app doesn't have location permission? iOS Swift4 没有在应用程序上请求许可 - iOS Swift4 Didn't Ask for Permission on App 在 iOS 移动浏览器中请求位置权限弹出窗口 - Ask for location permission popup in iOS mobile browser 不会弹出用于位置权限的iOS警报视图 - iOS alertview for location permission doesn't pop up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM