ios - CoreLocation swift

标签 ios swift cllocationmanager cllocation

我已经将 CoreLocation 库和 NSLocationWhenInUseUsageDescription 添加到 info.plist,但是当我编译和运行时,它不显示当前位置或提示用户允许访问当前位置。我目前有以下内容,但它只显示整个美国。和

The authorization status of location services is changed to: Not determined

已打印到日志中,但系统没有提示我允许访问当前位置,也没有收到警报。我尝试按照此处的逻辑和流程图进行操作:Xcode warning when using MapKit and CoreLocation但它没有用。

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)

}

enter image description here enter image description here enter image description here

最佳答案

首先,确保你在 plist 中有这个 key enter image description here

其次,在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")
        }
        
}

并得到输出 enter image description here

你必须在设备上运行这个,如果你不能得到警报,尝试卸载应用程序,然后再次运行

关于ios - CoreLocation swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30720928/

相关文章:

ios - 四舍五入到特定值?

iOS Swift 4 - 在 tableview didSelectRowAt 上显示 Toast(或事件指示器)

ios - 如何使用 Swift 以编程方式粘贴?

ios - Xcode 6 GM - CLLocationManager

ios - Remove Child From Firebase 快速删除整个节点

ios - DDMathParser 和 iOS7/XCode 兼容性

ios - 谷歌将自动完成的iOS快速与UITextField集成

ios - 为什么在 swift 中重新启动应用程序时会清除核心数据?

ios - 暂停直到 CLLocation 不为零

iphone - 使用 CNContactStore 以编程方式将家庭地址保存到新联系人?雨燕3