info.plist文件:

<key>NSLocationWhenInUseUsageDescription</key>
<string>This project would like to know your location</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>location when app is in background</string>

如上所示,已经为info.plist添加了键/值对

我面临的问题是,我第一次运行该应用程序时,它提示我进行授权,但是在随后的运行中,没有出现弹出窗口。

其次,我正在尝试从中打印位置
使用func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])print(location),但不打印任何内容。

在调试选项中,我将位置设置为“城市骑行”

我正在使用的代码如下。
import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate , CLLocationManagerDelegate {
@IBOutlet var map: MKMapView!

//use the locManager to get the users location

var locManager = CLLocationManager()

override func viewDidLoad() {
    locManager.delegate = self
    locManager.desiredAccuracy = kCLLocationAccuracyBest
    locManager.requestWhenInUseAuthorization()
    super.viewDidLoad()

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print(locations)
}

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


}

最佳答案

iOS将只向询问一次位置授权。如果用户拒绝,则他将不得不在设备设置中手动更改此设置。如果他同意,您将可以获取位置,直到他在设备设置中将其阻止为止。

所以这段代码:

locManager.requestWhenInUseAuthorization()

只会提示一次弹出 1次。问题是您可能忘了打电话:
locManager.startUpdatingLocation()

每当您要开始获取位置时,都需要调用此方法。

关于ios - 无法查看位置:[CLLocation],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36734672/

10-14 11:58
查看更多