【问题标题】:Xcode Location access not allowed不允许 Xcode 位置访问
【发布时间】:2017-11-12 10:47:49
【问题描述】:

我正在使用带有 Swift 3 的 Xcode 8.2.1,在我的应用程序中有一个地图视图,用户应该可以在其中按下按钮,地图将放大他们的位置。

在 Info.plist 中,我添加了两行:值为“asdf”的 NSLocationAlwaysUsageDescription 和值为“fdas”的 Privacy - Location When In Use Usage Description。

在运行应用程序之前,我选择“模拟位置”并选择预设位置。但是,每当用户按下按钮时,地图就会缩放到海洋中的某个随机位置,并且控制台会打印以下消息:

应用的 Info.plist 必须包含一个 NSLocationAlwaysUsageDescription 带有字符串值的键,向用户解释应用程序如何使用它 数据

这是我的整个 info.plist 源代码:

`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>Need your location </key>
    <string>asdf</string>
    <key>Need your location</key>
    <string>fdas</string>
</dict>
</plist>`

这里也是函数:

> func zoomOnLocation(sender: UIBarButtonItem) {
        print("zoomOnLocation running")
        /* Tracks user location */
        var locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        mapView.showsUserLocation = true
        if (CLLocationManager.locationServicesEnabled()) { //Check for Location Services
            locationManager = CLLocationManager()
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        }

        //Zoom to user location
        let noLocation = CLLocationCoordinate2D()
        let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200)
        mapView.setRegion(viewRegion, animated: false)

        DispatchQueue.main.async {
            locationManager.startUpdatingLocation()
        }

    }

非常感谢任何帮助!

【问题讨论】:

    标签: ios xcode swift3 mkmapview


    【解决方案1】:

    听起来您在 info.plist 中混合了语法。您说您添加的条目之一是作为属性列表属性编写的,另一个作为源。当我在使用定位服务的应用程序中右键单击我的 info.plist,然后选择 Open As... 然后选择 Source Code 我看到这些:

    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Need your location for this app!</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>This app needs your location</string>

    确保您的 info.plist 在被视为源代码时包含与这些类似的条目应该可以解决您的问题。

    属性列表中的等效条目是 Privacy - Location When In Usage DescriptionPrivacy - Location Always Use Description

    【讨论】:

    • 我的也一样...我将包括在问题中。
    • 虽然它们不一样,这两个 字段中的文本需要与我的两个字段中的文本相同,而不是“需要您的位置”。
    • 嗯,好的,现在修好了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2020-05-03
    相关资源
    最近更新 更多