Sea*_*itz 28 cllocationmanager swift
我试图将ObjC中的旧应用程序转换为Swift作为练习练习并且遇到了一些问题.我在旧应用程序中使用它的方式,它是建立CLLocation Manager然后我会使用:
manager = [[CLLocationManager alloc]init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation]
Run Code Online (Sandbox Code Playgroud)
会自动调用:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
}
Run Code Online (Sandbox Code Playgroud)
从那里我可以提取我需要的所有信息.但是在swift中,没有自动完成这种方法,我无法弄清楚如何重现它.文档说明了这一点
startUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)
仍将由代表调用,但它没有发生.
这是我到目前为止:
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"
}
}
Run Code Online (Sandbox Code Playgroud)
任何关于在哪里寻找的帮助或指示将不胜感激.谢谢.
编辑:从建议更新,但仍然无法正常工作
EDIT2:似乎是一些错误,不允许该方法在ViewController中正常工作
小智 64
你错过了两件事.首先,您必须使用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()
}
Run Code Online (Sandbox Code Playgroud)
其次,Info.plist 按照此处的说明编辑您的.
jay*_*iya 22
首先在plist文件中添加这两行
1)NSLocationWhenInUseUsageDescription
2)NSLocationAlwaysUsageDescription
然后这是班级工作完成这个
import UIKit
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
initLocationManager();
return true
}
// Location Manager helper stuff
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.locationServicesEnabled
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}
// Location Manager Delegate stuff
// If failed
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
if (error) {
if (seenError == false) {
seenError = true
print(error)
}
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
if (locationFixAchieved == false) {
locationFixAchieved = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
var coord = locationObj.coordinate
println(coord.latitude)
println(coord.longitude)
}
}
// authorization status
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var shouldIAllow = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access"
shouldIAllow = true
}
NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
if (shouldIAllow == true) {
NSLog("Location to Allowed")
// Start location services
locationManager.startUpdatingLocation()
} else {
NSLog("Denied access: \(locationStatus)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定为什么,但似乎startUpdatingLocation没有在iOS 7模拟器上显示用户提示,但是当我手动启用它时,如果我使用较新形式的委托方法,它按预期工作:
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]) { // Updated to current array syntax [AnyObject] rather than AnyObject[]
println("locations = \(locations)")
}
Run Code Online (Sandbox Code Playgroud)
您使用的格式自iOS 5或6以来已被弃用,因此显然它不受快速桥接层的支持.
| 归档时间: |
|
| 查看次数: |
87978 次 |
| 最近记录: |