不允许Xcode位置访问

时间:2017-06-10 14:20:13

标签: ios xcode swift3 mkmapview

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

在Info.plist中,我添加了两行:NSLocationAlwaysUsageDescription,其值为" asdf"和隐私 - 使用时的位置用法描述的值" fdas"。

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

  

应用程序的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()
        }

    }

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

听起来好像是在info.plist中混合语法。您说已添加的其中一个条目被写为属性列表属性,另一个作为源。当我使用位置服务在应用程序中右键单击我的info.plist,然后选择打开为... 然后源代码时,我看到了这些:

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

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

属性列表中的等效条目将是隐私 - 使用说明中的位置隐私 - 位置始终使用说明