【发布时间】: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