无法从 iOS 模拟器获取位置
Cannot get position from iOS simulator
我正在尝试通过简单的 swift 代码使用 iOS 模拟器来获取位置。但是在调试会话中,没有调用回调函数。我将虚拟位置设置为 iOS 模拟器。
// ViewController.swift
// helloWorld
//
// Created by on 5/17/15.
// Copyright (c) 2015 All rights reserved.
//
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locMan: CLLocationManager!
var lat: CLLocationDegrees!
var lng: CLLocationDegrees!
var hasPosData: Bool
required init(coder aDecoder: NSCoder) {
locMan = CLLocationManager()
lat = CLLocationDegrees()
lng = CLLocationDegrees()
hasPosData = false
super.init(coder: aDecoder)
}
func retreiveCurrentPos(){
locMan.delegate = self
locMan.requestAlwaysAuthorization()
// start using GPS function
locMan.startUpdatingLocation()
}
func saveResult2File(){
var contents: String
let dirToSave = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as! Array<String>
let file = String(dirToSave[0]) + "pos.dat"
if(hasPosData){
contents = String(format:"lat: %f lng: %f", lat, lng)
}else{
contents = "Failed to retreive position data"
}
contents.writeToFile(file, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
retreiveCurrentPos()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
lat = newLocation.coordinate.latitude
lng = newLocation.coordinate.longitude
hasPosData = true
saveResult2File()
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
hasPosData = false
saveResult2File()
}
}
您需要在您的 iOS 模拟器中设置一个位置,它默认设置为 none 导致应用程序崩溃的原因是您在使用前没有检查它。
此外,您还需要在 pList NSLocationWhenInUseUsageDescription 中添加一个键,以防您忘记。
plist
我正在尝试通过简单的 swift 代码使用 iOS 模拟器来获取位置。但是在调试会话中,没有调用回调函数。我将虚拟位置设置为 iOS 模拟器。
// ViewController.swift
// helloWorld
//
// Created by on 5/17/15.
// Copyright (c) 2015 All rights reserved.
//
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locMan: CLLocationManager!
var lat: CLLocationDegrees!
var lng: CLLocationDegrees!
var hasPosData: Bool
required init(coder aDecoder: NSCoder) {
locMan = CLLocationManager()
lat = CLLocationDegrees()
lng = CLLocationDegrees()
hasPosData = false
super.init(coder: aDecoder)
}
func retreiveCurrentPos(){
locMan.delegate = self
locMan.requestAlwaysAuthorization()
// start using GPS function
locMan.startUpdatingLocation()
}
func saveResult2File(){
var contents: String
let dirToSave = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as! Array<String>
let file = String(dirToSave[0]) + "pos.dat"
if(hasPosData){
contents = String(format:"lat: %f lng: %f", lat, lng)
}else{
contents = "Failed to retreive position data"
}
contents.writeToFile(file, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
retreiveCurrentPos()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
lat = newLocation.coordinate.latitude
lng = newLocation.coordinate.longitude
hasPosData = true
saveResult2File()
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
hasPosData = false
saveResult2File()
}
}
您需要在您的 iOS 模拟器中设置一个位置,它默认设置为 none 导致应用程序崩溃的原因是您在使用前没有检查它。 此外,您还需要在 pList NSLocationWhenInUseUsageDescription 中添加一个键,以防您忘记。
plist