我试图将ObjC中的一个旧应用程序转换为Swift作为练习练习,并且遇到了一些问题。我在旧应用程序中的方式,它正在建立CLLocation Manager,然后我会使用:
manager = [[CLLocationManager alloc]init]; manager.delegate = self; manager.desiredAccuracy = kCLLocationAccuracyBest; [manager startUpdatingLocation]
-(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@
你缺少两件事情首先,您必须使用requestAlwaysAuthorization或requestWhenInUseAuthorization()请求许可。所以您的viewDidLoad()应该是这样的:
原文链接:https://www.f2er.com/swift/320682.htmloverride 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@