创建一个 swift 助手 class 来处理 CoreLocation 函数
Creating a swift helper class to handle CoreLocation functions
我是 Swift 的新手,我已经成功创建了一个入门应用程序来获取当前的 GPS 位置。现在我正在尝试创建一个助手 class (Location.swift),它将执行所有基于 GPS 位置的功能,并且可以从我的主程序 ViewController 中调用(例如 Location.getGPSLocation( ) 其中 getGPSLocation 是我的 Location.swift 助手 class) 中的静态函数。
请查看我的 Location.swift 助手代码 class:
import Foundation
import CoreLocation
public class Location
{
private static let locationManager = CLLocationManager()
public static func getGPSLocation()
{
let authorizationStatus = CLLocationManager.authorizationStatus()
if (authorizationStatus == .notDetermined)
{
locationManager.requestWhenInUseAuthorization()
return
} else if (authorizationStatus == .denied || authorizationStatus == .restricted)
{
print("ERROR: Please enable location services")
return
} else
{
startLocationManager()
}
}
private static func startLocationManager()
{
if CLLocationManager.locationServicesEnabled()
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
}
我现在收到上述代码的错误消息 "Cannot assign value of type 'Location.Type' to type 'CLLocationManagerDelegate?"。
我该如何解决这个问题?
干杯!
首先你的Location
对象需要继承NSObject
和CLLocationManagerDelegate
。其次,您需要提供一个 shared
实例 属性 并将您的管理器声明从静态更改为一个实例 属性。第三次覆盖您的 Location
初始化程序,调用 super 然后启用您的位置服务并在那里设置您的经理委托:
import CoreLocation
class Location: NSObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
static let shared = Location()
var location: CLLocation?
private override init() {
super.init()
if CLLocationManager.locationServicesEnabled() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.distanceFilter = 10
}
}
static func requestGPS() {
Location.shared.manager.requestLocation()
}
}
现在您可以实现扩展 Location 的委托方法:
extension Location {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.location = locations.last
print("location", location ?? "")
}
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
print(#function, "status", status)
// check the authorization status changes here
}
func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
print(#function)
if (error as? CLError)?.code == .denied {
manager.stopUpdatingLocation()
manager.stopMonitoringSignificantLocationChanges()
}
}
}
我是 Swift 的新手,我已经成功创建了一个入门应用程序来获取当前的 GPS 位置。现在我正在尝试创建一个助手 class (Location.swift),它将执行所有基于 GPS 位置的功能,并且可以从我的主程序 ViewController 中调用(例如 Location.getGPSLocation( ) 其中 getGPSLocation 是我的 Location.swift 助手 class) 中的静态函数。
请查看我的 Location.swift 助手代码 class:
import Foundation
import CoreLocation
public class Location
{
private static let locationManager = CLLocationManager()
public static func getGPSLocation()
{
let authorizationStatus = CLLocationManager.authorizationStatus()
if (authorizationStatus == .notDetermined)
{
locationManager.requestWhenInUseAuthorization()
return
} else if (authorizationStatus == .denied || authorizationStatus == .restricted)
{
print("ERROR: Please enable location services")
return
} else
{
startLocationManager()
}
}
private static func startLocationManager()
{
if CLLocationManager.locationServicesEnabled()
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
}
我现在收到上述代码的错误消息 "Cannot assign value of type 'Location.Type' to type 'CLLocationManagerDelegate?"。
我该如何解决这个问题?
干杯!
首先你的Location
对象需要继承NSObject
和CLLocationManagerDelegate
。其次,您需要提供一个 shared
实例 属性 并将您的管理器声明从静态更改为一个实例 属性。第三次覆盖您的 Location
初始化程序,调用 super 然后启用您的位置服务并在那里设置您的经理委托:
import CoreLocation
class Location: NSObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
static let shared = Location()
var location: CLLocation?
private override init() {
super.init()
if CLLocationManager.locationServicesEnabled() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.distanceFilter = 10
}
}
static func requestGPS() {
Location.shared.manager.requestLocation()
}
}
现在您可以实现扩展 Location 的委托方法:
extension Location {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.location = locations.last
print("location", location ?? "")
}
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
print(#function, "status", status)
// check the authorization status changes here
}
func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
print(#function)
if (error as? CLError)?.code == .denied {
manager.stopUpdatingLocation()
manager.stopMonitoringSignificantLocationChanges()
}
}
}