我正在尝试在Swift 4中制作Geofence,但是我有一个问题,在调用locationManager时出现错误,我不明白为什么会收到此错误
这是错误:
歧义使用locationManager
我试图通过放置self.locationManager.startUpdatingLocation()和其他不同的方法来纠正它,但是没有一个起作用
在这里,我声明了变量locationManager:
var locationManager = CLLocationManager()
if let appDelegate = UIApplication.shared.delegate as? AppDelegate{
locationManager = appDelegate.locationManager
locationManager.delegate = self
}
这是我收到错误的代码块:
if let locationManager = self.locationManager{
let region = self.regionToMonitor()
if(locationManager.monitoredRegions.count > 0){
self.geofencesLabel.text = "Geofences OFF"
locationManager.stopMonitoring(for: region)
mapView.removeOverlays(mapView.overlays)
}else{
self.geofencesLabel.text = "Geofences ON"
locationManager.startMonitoring(for: region)
mapView.add(MKCircle(center: region.center, radius: region.radius))
nombreU.isHidden = true
empresaU.isHidden = true
correoElectronicoU.isHidden = true
telefonoU.isHidden = true
//Generamos el registro en la base de datos local en el servidor
let nombreEvento = self.nombreE
let nombreOficial = self.nombreO
let fechaEvento = self.fechaE
let fechaFormato = self.fechaF
let lugarEvento = self.lugarE
let descripcionEvento = self.descripcionE
let imagenEvento = self.imagenE
let latitudEvento = self.latitudE
let longitudEvento = self.longitudE
let empresaEvento = self.empresaE
let rolEvento = self.rolE
//Recuperamos el contenido de las cajas de texto para validarlo
let nombreC = self.nombreU.text!
let empresaC = self.empresaU.text!
let correoC = self.correoElectronicoU.text!
let telefonoC = self.telefonoU.text!
//Validamos que no este vacio ninguna de los datos recibidos del servidor y ninguna de las cajas de texto
if(nombreC.isEmpty || empresaC.isEmpty || correoC.isEmpty || telefonoC.isEmpty){
displayMyAlertMessage(userMessage: "Todos los campos deben ser llenados")
return
}
if(nombreEvento.isEmpty || nombreOficial.isEmpty || fechaEvento.isEmpty || fechaFormato.isEmpty || lugarEvento.isEmpty || descripcionEvento.isEmpty){
displayMyAlertMessage(userMessage: "Ocurrio un error al cargar los eventos")
dismiss(animated: true, completion: nil)
return
}
//Ingresamos los valores a la base de datos local
let newEvento = NSEntityDescription.insertNewObject(forEntityName: "Eventos", into: context)
newEvento.setValue(nombreEvento, forKey: "nombreE")
newEvento.setValue(nombreOficial, forKey: "nombreO")
newEvento.setValue(fechaEvento, forKey: "fechaE")
newEvento.setValue(fechaFormato, forKey: "fechaF")
newEvento.setValue(lugarEvento, forKey: "lugarE")
newEvento.setValue(descripcionEvento, forKey: "descripcionE")
newEvento.setValue(imagenEvento, forKey: "imagenE")
newEvento.setValue(latitudEvento, forKey: "latitudE")
newEvento.setValue(longitudEvento, forKey: "longitudE")
newEvento.setValue(empresaEvento, forKey: "empresaE")
newEvento.setValue(rolEvento, forKey: "rolE")
nombreU.isHidden = true
empresaU.isHidden = true
correoElectronicoU.isHidden = true
telefonoU.isHidden = true
//Mostramos mensaje de que ya estas registrado
mensajeRegistro.text = "Gracias por registrarte a nuestro evento"
mensajeRegistro.isHidden = false
do{
try context.save()
print("El registro al evento se realizo de forma correcta")
}catch{
print(error)
}
//Ejecutamos el codigo del WebService
guard let url = URL(string: "http://www.sitioweb.mx/webservice/registro.php?nombre=\(nom)&empresa=\(empresaC)&correo=\(corC)&telefono=\(telC)") else { return }
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if let response = response {
print(response)
}else{
print(error!)
}
}
}
}else{
notify(msg: "El geofence no esta disponible")
}
我班有这个扩展:
// MARK: - Location Manager Delegate
extension RegistroViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
mapView.showsUserLocation = status == .authorizedAlways
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
print("Monitoring failed for region with identifier: \(region!.identifier)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location Manager failed with the following error: \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Location update")
}
}
我不明白什么是错误的,或者在调用self.locationManager时应该引用什么方法
这是我的Appdelegate:
import UIKit
import CoreData
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static var menu_bool = true
static var menu_boolS = true
let locationManager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
locationManager.delegate = self as? CLLocationManagerDelegate
locationManager.requestAlwaysAuthorization()
let options: UNAuthorizationOptions = [.badge, .sound, .alert]
UNUserNotificationCenter.current().requestAuthorization(options: options){ success, error in
if let error = error{
print("Error: \(error)")
}
}
return true
}
最佳答案
如果locationManager
是实例属性,请直接使用它
let locationManager = CLLocationManager()
let region = self.regionToMonitor()
if(locationManager.monitoredRegions.count > 0){
self.geofencesLabel.text = "Geofences OFF"
locationManager.stopMonitoring(for: region)
mapView.removeOverlays(mapView.overlays)
}else{
self.geofencesLabel.text = "Geofences ON"
locationManager.startMonitoring(for: region)
mapView.add(MKCircle(center: region.center, radius: region.radius))
不带
if let locationManager = self.locationManager{
关于ios - 我有此错误:不确定使用“locationManager”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56330168/