11.5 C
London
Friday, March 29, 2024

swift – Exercise detection in iOS – Exercise Supervisor need to detect automotive exercise in background,foreground and app terminate states


Created the placement supervisor in that I’ll fetch Vital Location Modifications. In that location delegate didupdate location technique I’m calling exercise supervisor.

func locationManager(_ supervisor: CLLocationManager, didUpdateLocations places: [CLLocation]) {
    self.startActivityUpdates()
    let userLocation:CLLocation = places[0] as CLLocation
    let middle = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    if automotive && updateLocation
    {
        tripcoords.append(middle)
    }
    
}

In that exercise supervisor I’m detecting whether or not exercise is automotive. If automative means I’m updating location in tripcoords array.

func startActivityUpdates() {
    if CMMotionActivityManager.isActivityAvailable() {
        let historyDuration = 5 // Set historical past length to 60 seconds
        activityManager.queryActivityStarting(from: Date(timeIntervalSinceNow: -TimeInterval(historyDuration)), to: Date(), to: motionQueue) { [weak self] (actions, error) in
            guard let actions = actions, error == nil else {
                print("Error querying actions: (error?.localizedDescription ?? "Unknown error")")
                return
            }
            if let latestActivity = actions.final {
                DispatchQueue.principal.async {
                    self?.handleActivity(latestActivity)
                }
            }
        }
    } else {
        print("Exercise updates not accessible")
    }
}

Some instances the placement updates within the journeys array some instances lacking. Kindly assist me to get. Thanks prematurely!

func handleActivity(_ exercise: CMMotionActivity) {
    var activityString = ""
    if exercise.stationary  {
        self.updateLocation = true
        activityString = "Stationary"
    } else if exercise.strolling  {
        print("Strolling")
        self.endTrip = true
        self.updateCompletedTrips()
        activityString = "Strolling"
    } else if exercise.working {
        self.endTrip = true
        self.updateCompletedTrips()
        activityString = "Operating"
    } else if exercise.automotive {
        print("Automotive new technique")
        self.endTrip = false
        self.startTripTime = self.startTripTime == "" ?  Date.getCurrentDate(dateformat: true) : self.startTripTime
        self.automotive = true
        self.updateLocation = true
        activityString = "Automotive"
    } else if exercise.biking {
        print("Biking")
        self.endTrip = true
        self.updateCompletedTrips()
        activityString = "Biking"
    } else if exercise.unknown {
        print("Unknown exercise")
    }
}

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here