CoreLocation 斯威夫特

Posted

技术标签:

【中文标题】CoreLocation 斯威夫特【英文标题】:CoreLocation Swift 【发布时间】:2015-06-08 23:58:39 【问题描述】:

我已将 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)


【问题讨论】:

【参考方案1】:

首先,确保你在 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 中下断点来检查requestWhenInUseAuthorization 是否被调用? 它没有被调用。 尝试删除NSLocationAlwaysUseDescription,你不需要在pist中同时添加 没什么区别。

以上是关于CoreLocation 斯威夫特的主要内容,如果未能解决你的问题,请参考以下文章

CoreLocation 负值

CoreLocation框架之APP定位

无法通过 Corelocation 找到位置

iOS核心笔记—CoreLocation框架-基础

MKMapView 不专注于 CoreLocation

检测 App 是不是链接到 CoreLocation 框架