Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> iOS

วิธีตรวจสอบ CLLocationManager ทำงานอยู่หรือไม่ใน iOS App

CLLocationManager ไม่มี Property ตรวจสอบว่า "กำลังทำงานอยู่" โดยตรง แต่สามารถเช็คได้จากสถานะต่างๆ

สิ่งที่ตรวจสอบได้

1. Location Services เปิดอยู่หรือไม่ (System Level)


if CLLocationManager.locationServicesEnabled() {
    print("Location Services: ON")
} else {
    print("Location Services: OFF")
}

2. Authorization Status (App Level)


let status = CLLocationManager.authorizationStatus()
switch status {
case .authorizedAlways: print("Authorized Always (Background OK)")
case .authorizedWhenInUse: print("Authorized When In Use")
case .denied: print("Denied")
case .restricted: print("Restricted")
case .notDetermined: print("Not Determined")
@unknown default: break
}

3. Delegate Callback (Indirect Check)


class LocationManager: NSObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()
    private var isUpdating = false
    
    func startUpdating() {
        guard CLLocationManager.locationServicesEnabled() else { return }
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.startUpdatingLocation()
        isUpdating = true
    }
    
    func stopUpdating() {
        manager.stopUpdatingLocation()
        isUpdating = false
    }
    
    // Delegate - รับรู้ว่ากำลังรับ Location
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // ถ้ามี Delegate Call แสดงว่ากำลังทำงาน
        print("Receiving locations: \(locations.last?.coordinate ?? .zero)")
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Location Error: \(error)")
    }
}

Best Practice: State Management


class LocationManager: NSObject, CLLocationManagerDelegate {
    enum State { case idle, requesting, authorized, updating, error(Error) }
    
    private(set) var state: State = .idle {
        didSet { print("Location State: \(state)") }
    }
    
    func requestAndStart() {
        state = .requesting
        let status = CLLocationManager.authorizationStatus()
        switch status {
        case .notDetermined:
            manager.requestWhenInUseAuthorization()
        case .authorizedWhenInUse, .authorizedAlways:
            startUpdating()
        default:
            state = .error(LocationError.notAuthorized)
        }
    }
    
    func startUpdating() {
        manager.startUpdatingLocation()
        state = .updating
    }
    
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        requestAndStart()
    }
}

Significant Location Changes (Background)


// ตรวจสอบว่ากำลัง Monitor อยู่
// ไม่มี API ตรวจสอบโดยตรง ต้อง Track เอง
var isMonitoringSignificant = false

func startSignificantChanges() {
    manager.startMonitoringSignificantLocationChanges()
    isMonitoringSignificant = true
}

func stopSignificantChanges() {
    manager.stopMonitoringSignificantLocationChanges()
    isMonitoringSignificant = false
}

สรุป

  • ไม่มี isRunning Property
  • เช็ค locationServicesEnabled() + authorizationStatus()
  • Track State เองผ่าน Delegate Calls
  • ใช้ startUpdatingLocation() ซ้ำไม่สร้าง Instance ใหม่ (Apple Docs)