cllocationmanager – Swift中的CLLocation Manager获取用户位置

前端之家收集整理的这篇文章主要介绍了cllocationmanager – Swift中的CLLocation Manager获取用户位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将ObjC中的一个旧应用程序转换为Swift作为练习练习,并且遇到了一些问题。我在旧应用程序中的方式,它正在建立CLLocation Manager,然后我会使用:
manager = [[CLLocationManager alloc]init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;    
[manager startUpdatingLocation]

这将自动调用:@H_404_4@

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
}

从那里我可以提取我需要的所有信息。但是,快速的,没有这种方法自动完成,我无法弄清楚如何重现它。文件说@H_404_4@

startUpdatingLocation()

仍将被代表召集,但并没有发生。@H_404_4@

这是我到目前为止:@H_404_4@

import UIKit
import corelocation

class ViewController: UIViewController,CLLocationManagerDelegate{

@IBOutlet var gpsResult : UILabel

var manager:CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,typically from a nib.

    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.startUpdatingLocation()
}

func locationManager(manager:CLLocationManager,didUpdateLocations locations:AnyObject[]) {
    println("locations = \(locations)")
    gpsResult.text = "success"
}
}

任何帮助或指针,在哪里可以欣赏。谢谢。@H_404_4@

编辑:从建议更新,但仍然不工作@H_404_4@

EDIT2似乎是一些错误,不允许该方法在ViewController中正常工作@H_404_4@

你缺少两件事情首先,您必须使用requestAlwaysAuthorization或requestWhenInUseAuthorization()请求许可。所以您的viewDidLoad()应该是这样的:
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,typically from a nib.
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
}

二,编辑您的Info.plist as indicated here。@H_404_4@

原文链接:https://www.f2er.com/swift/320682.html

猜你在找的Swift相关文章