swift - 如何编写用于检查用户位置的 Swift 代码并仅在用户位于地理围栏半径内时才执行该功能?
问题描述
目标是仅在当前用户位置在预定义的地理围栏内时才允许通过点击“我在这里”发布数据。如果他们超出界限,他们将收到警报错误,并且数据不会发布到 Firestore。
我已经阅读了有关 didDetermineState 的信息,但无法理解。
import UIKit
import Firebase
import FirebaseFirestore
import MapKit
import CoreLocation
class checkInViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var imHereButton: UIButton!
@IBOutlet weak var callUsButton: UIButton!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setUpElements()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
let geoFenceRegion:CLCircularRegion = CLCircularRegion(center: CLLocationCoordinate2DMake(xxxx, xxxx), radius: 5, identifier: "xxxx")
locationManager.startMonitoring(for: geoFenceRegion)
}
func setUpElements (){
Utilities.styleFilledButton(imHereButton)
Utilities.styleFilledButton(callUsButton)
}
@IBAction func backTapped(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Check-In Enabled")
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Check-In Disabled")
}
@IBAction func imHereTapped(_ sender: Any) {
guard let uid = Auth.auth().currentUser?.uid else { return }
guard let memberNumber = UserDefaults.standard.string(forKey: "memberNumber"), !memberNumber.isEmpty else
{ return }
guard let firstName = UserDefaults.standard.string(forKey: "firstName"), !firstName.isEmpty else
{ return }
guard let lastName = UserDefaults.standard.string(forKey: "lastName"), !lastName.isEmpty else
{ return }
let checkInData = ["name":"\(firstName) \(lastName)", "memberNumber":memberNumber, "userId": uid]
FirestoreReferenceManager.myReferenceForCheckIn().addDocument(data: checkInData) { (error) in
if error != nil {
print("Error, Data not saved")
} else {
self.showResAlert(title: "Welcome Back", message: "You are now CHECKED-IN please continue to the rental counter. Thank You.")
}
}
}
@IBAction func callUsTapped(_ sender: Any) {
if let url = URL(string: "tel://\(xxxx)"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
}
extension checkInViewController: CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.stopUpdatingLocation()
mapView.showsUserLocation = true
}
}
解决方案
推荐阅读
- freemarker - freemarker中的值总和
- java - 我们可以在 Spring Boot 应用程序中创建两个具有不同用户的相同数据库的会话工厂吗
- haproxy - HAProxy 平衡 b/w 多个后端不起作用
- java - 如何在 JUnit 中模拟静态方法
- python - 如何让 asksaveasfilename 将文件仅保存为图像并在文件目录中访问?
- php - PHP 电子表格 - 获得下划线效果
- gcc - GCC有:找不到命令[:参数太多
- html - 如何将悬停效果应用于特定按钮
- javascript - Nestjs Redis 微服务 cron 任务与 this.client.send
- firebase - 如何仅将具有查看者权限的新用户添加到 console.firebase 项目中的特定应用程序