Ios 岩芯定位雨燕
我已将CoreLocation库以及NSLocationWhenUsageDescription添加到info.plist,但在编译和运行时,它不会显示当前位置或提示用户允许访问当前位置。我目前有以下内容,但它只显示了整个美国。及Ios 岩芯定位雨燕,ios,swift,cllocationmanager,cllocation,Ios,Swift,Cllocationmanager,Cllocation,我已将CoreLocation库以及NSLocationWhenUsageDescription添加到info.plist,但在编译和运行时,它不会显示当前位置或提示用户允许访问当前位置。我目前有以下内容,但它只显示了整个美国。及 The authorization status of location services is changed to: Not determined 已打印到日志中,但未提示我允许访问当前位置,也未收到警报。我试着按照这里的逻辑和流程图进行操作:但没有成功 imp
The authorization status of location services is changed to: Not determined
已打印到日志中,但未提示我允许访问当前位置,也未收到警报。我试着按照这里的逻辑和流程图进行操作:但没有成功
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{
var loadPoints:[Int: MapPointAnnotation] = [Int:MapPointAnnotation]()
var map:MKMapView?
var loads: [Load]?
var rightButton: UIButton?
var selectedLoad:Load?
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Core Location
manager = CLLocationManager()
manager.delegate = self
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus){
print("The authorization status of location " +
"services is changed to: ")
switch CLLocationManager.authorizationStatus(){
case .Denied:
println("Denied")
case .NotDetermined:
println("Not determined")
case .Restricted:
println("Restricted")
default:
println("Authorized")
}
}
func displayAlertWithTitle(title: String, message: String){
let controller = UIAlertController(title: title,
message: message,
preferredStyle: .Alert)
controller.addAction(UIAlertAction(title: "OK",
style: .Default,
handler: nil))
presentViewController(controller, animated: true, completion: nil)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if CLLocationManager.locationServicesEnabled(){
switch CLLocationManager.authorizationStatus(){
case .Denied:
displayAlertWithTitle("Not Determined",
message: "Location services are not allowed for this app")
case .NotDetermined:
manager = CLLocationManager()
if let locationManager = self.manager{
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
case .Restricted:
displayAlertWithTitle("Restricted",
message: "Location services are not allowed for this app")
default:
println("Default")
}
} else {
println("Location services aren't enabled")
}
}
convenience init(frame:CGRect){
self.init(nibName: nil, bundle: nil)
self.view.frame = frame
NSNotificationCenter.defaultCenter().addObserver(self, selector:"selectAnnotation:", name: "selectAnnotation", object: nil)
self.map = MKMapView(frame: frame)
self.map!.delegate = self
self.view.addSubview(self.map!)
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
var userLocation:CLLocation = locations[0] as! CLLocation
var latitude:CLLocationDegrees = userLocation.coordinate.latitude
var longitude:CLLocationDegrees = userLocation.coordinate.longitude
var latDelta:CLLocationDegrees = 1.0
var lonDelta:CLLocationDegrees = 1.0
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map!.setRegion(region, animated: true)
}
首先,确保您在plist中有此密钥 其次,在
viewDidLoad
中请求位置服务,我用这些代码进行测试
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager.delegate = self
if(NSString(string:UIDevice.currentDevice().systemVersion).doubleValue >= 8.0){
manager.requestWhenInUseAuthorization()
}
// Do any additional setup after loading the view, typically from a nib.
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus){
print("The authorization status of location " +
"services is changed to: ")
switch CLLocationManager.authorizationStatus(){
case .Denied:
println("Denied")
case .NotDetermined:
println("Not determined")
case .Restricted:
println("Restricted")
default:
println("Authorized")
}
}
并获得输出
您必须在设备上运行此应用程序,如果无法获得警报,请尝试卸载该应用程序,然后再次运行。首先,请确保您在plist中有此密钥 其次,在
viewDidLoad
中请求位置服务,我用这些代码进行测试
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager.delegate = self
if(NSString(string:UIDevice.currentDevice().systemVersion).doubleValue >= 8.0){
manager.requestWhenInUseAuthorization()
}
// Do any additional setup after loading the view, typically from a nib.
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus){
print("The authorization status of location " +
"services is changed to: ")
switch CLLocationManager.authorizationStatus(){
case .Denied:
println("Denied")
case .NotDetermined:
println("Not determined")
case .Restricted:
println("Restricted")
default:
println("Authorized")
}
}
并获得输出
您必须在设备上运行此程序,如果无法获得警报,请尝试卸载该应用程序,然后再次运行,因此,我更新了我的代码并拍摄了它的快照。你能在viewDidLoad中设置一个断点来检查调用InUseAuthorization时是否请求了
吗?它没有被调用。尝试删除NSLocationAlwaysUseDescription
,你不需要在pistIt中同时添加这两个选项。我尝试了更新的版本,但它不起作用,因此,我更新了我的代码并拍摄了它的快照。你能在viewDidLoad中设置一个断点来检查调用InUseAuthorization
时是否请求了吗?它没有被调用。尝试删除NSLocationAlwaysUseDescription
,你不需要在pistIt中同时添加这两个选项。我尝试了更新的版本,但它不起作用,因此,我更新了我的代码并对其进行了快照。在调用InUseAuthorization
时,您是否可以在viewDidLoad中设置断点以检查请求是否被调用?它没有被调用。请尝试删除NSLocationAlwaysUseDescription
,您不需要在pistIt中同时添加这两个属性,这并没有什么区别。