gpt4 book ai didi

ios - 无法使用带有 NSLocationWhenInUseUsageDescription 的 Swift 和 iOS8 获取位置服务

转载 作者:行者123 更新时间:2023-11-28 07:09:16 25 4
gpt4 key购买 nike

我做了很多关于如何让定位服务在 Swift 和 iOS8 上正常工作的研究。我知道有很多主题描述了常见的陷阱,但对我没有任何帮助。

这是我的 View Controller 的代码:

import UIKit

class ViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {

//#2
var gmaps: GMSMapView?

let locationManager = CLLocationManager()

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func viewDidLoad() {
super.viewDidLoad()

var target: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 51.6, longitude: 17.2)
var camera: GMSCameraPosition = GMSCameraPosition(target: target, zoom: 6, bearing: 0, viewingAngle: 0)

gmaps = GMSMapView(frame: CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height))
if let map = gmaps? {
map.camera = camera
map.delegate = self

self.view.addSubview(gmaps!)

map.animateToZoom(10)
}

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()

if let map = gmaps? {
map.myLocationEnabled = true
map.settings.myLocationButton = true
}
}
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
if let map = gmaps? {
map.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

locationManager.stopUpdatingLocation()
}
}
}
}

在我的信息 plist 中,我设置了键 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 和 Privacy - Location Usage Description。我还将 CoreLocation.framework 添加到我的链接框架和库中。

但我仍然没有收到询问用户位置权限的弹出窗口。没有错误,但也没有其他事情发生:-(

最佳答案

您应该检查是否已为您的应用授予权限,如果是,请删除该权限(iOS 设置应用、隐私)。

另一个建议是从模拟器/设备上完全卸载该应用。

这是来 self 的 appDelegate 的代码片段,它使用 requestAlwaysAuthorization 但这应该不会有什么不同:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// intialize locationManager
locationManager.delegate = self
locationManager.activityType = CLActivityType.Fitness
locationManager.distanceFilter = 10 // 10m
locationManager.requestAlwaysAuthorization()

// get current location
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
locationManager.startUpdatingLocation()
}
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .Authorized {
locationManager.startUpdatingLocation()
}
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
...
}

这是在我的 Info.plist 中:

<key>NSLocationAlwaysUsageDescription</key>
<string>No tracking without your permission</string>

关于ios - 无法使用带有 NSLocationWhenInUseUsageDescription 的 Swift 和 iOS8 获取位置服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29121126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com