我试图使用XCode 7.1&amp ;;来抓住用户的位置。适用于iOS 9.1。
我没有在代码中看到任何错误,但每次尝试获取位置时我都会遇到同样的错误。 Error finding location: The operation couldn’t be completed. (kCLErrorDomain error 0.)
这是我的代码:
import UIKit
import AVFoundation
import CoreLocation
class secondview: UIViewController, CLLocationManagerDelegate {
var player = AVPlayer() // Audio Player
let locationManager = CLLocationManager() // Location Manager
override func viewDidLoad() {
super.viewDidLoad()
//PlayEntryAudio() // Start playing entry audio
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.requestLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print("Current location: \(location)")
} else {
// ...
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error finding location: \(error.localizedDescription)")
}
func PlayEntryAudio(){
print("Got here")
let url = NSURL(string: "https://9147abb4.ngrok.io/track.mp4")!
let playerItem = AVPlayerItem( URL:url)
player = AVPlayer(playerItem:playerItem)
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
编辑01:注意到我没有收到警告要求我获取位置,所以我尝试添加locationManager.requestWhenInUseAuthorization()
但没有成功。