Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift Geofence didExitRegion从未被称为_Swift_Xcode_Swiftui_Geofencing - Fatal编程技术网

Swift Geofence didExitRegion从未被称为

Swift Geofence didExitRegion从未被称为,swift,xcode,swiftui,geofencing,Swift,Xcode,Swiftui,Geofencing,我正在尝试在我正在开发的应用程序中实现地理围栏。目前,我无法通过考试 由于从未调用didExitRegion,因此需要输出到控制台 原因可能是什么?我该如何着手解决 import SwiftUI import MapKit struct MapView: UIViewRepresentable { var locationManager = CLLocationManager() func setupManager() { locationManage

我正在尝试在我正在开发的应用程序中实现地理围栏。目前,我无法通过考试 由于从未调用
didExitRegion
,因此需要输出到控制台

原因可能是什么?我该如何着手解决


import SwiftUI
import MapKit

struct MapView: UIViewRepresentable {

    var locationManager = CLLocationManager()


    func setupManager() {

        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        //locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()

        // Set Geofencing region
        let geofencingRegion: CLCircularRegion = CLCircularRegion(center: CLLocationCoordinate2DMake(30.510074, 114.330510), radius: 100, identifier: "Wuhan")
        geofencingRegion.notifyOnExit = true
        geofencingRegion.notifyOnEntry = true

        // Start monitoring
        locationManager.startMonitoring(for: geofencingRegion)
    }



    func makeUIView(context: Context) -> MKMapView {
        setupManager()
        let mapView = MKMapView(frame: UIScreen.main.bounds)
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .follow
        return mapView
        
    }


    func updateUIView(_ uiView: MKMapView, context: Context) {
    }
}

class MapAppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var locationManager: CLLocationManager?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        self.locationManager = CLLocationManager()
        self.locationManager!.delegate = self

        return true
    }
}

extension MapAppDelegate{

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("Hello World")
    }

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print("Welcome Home")
    }
}
我得到的输出是

2020-12-11 18:41:29.916937+0800 Geofencing[4225:235542] [VKDefault] Style Z is requested for an invisible rect

我想创建一个状态来检查用户是否已进入该区域或离开该区域,以在我的主视图上拖动其他视图控制器,因此我从这里开始。

从未调用
didExitRegion
的原因是您没有设置
CLLocationManager
委托

MapView
中,您正在创建
CLLocationManager
的实例,然后在此实例上调用
.startMonitoring
。但您从未在此实例上设置委托

确实,您正在
MapAppDelegate
中创建的实例上设置委托,但该实例不是您正在调用的
.startMonitoring


您应该重构代码,以便只创建
CLLocationManager
的单个实例,并确保在该实例上设置委托。

从未调用
didExitRegion
的原因是您没有设置
CLLocationManager
委托

MapView
中,您正在创建
CLLocationManager
的实例,然后在此实例上调用
.startMonitoring
。但您从未在此实例上设置委托

确实,您正在
MapAppDelegate
中创建的实例上设置委托,但该实例不是您正在调用的
.startMonitoring


你应该重构你的代码,只创建一个
CLLocationManager
实例,并确保在该实例上设置委托。

Hello@littleim,我有相同的控制台错误,你找到解决方案了吗?Hello@littleim,我有相同的控制台错误,你找到解决办法了吗?