I am creating a workout app in Apple Watch with the HKWorkoutSession and using HKLiveWorkoutBuilder to access the live data like distance, calories, heart rate. The user can pause and resume the workout as well. Suppose I pause the workout after 5 minutes and then kill or force quit the app. After it when I relaunch the app again after 5 minutes more then the builder elapsed time does not includes the pause duration because the activity is again started from the start time. How can I get the current working workout session that I last created before killing the application? This works well when the application is either in foreground or in background.
Post
Replies
Boosts
Views
Activity
I am creating a workout in Apple Watch and on stop, the workout is saved into the HealthKit. This is being done through the HKLiveWorkoutBuilder. I am only reading the distance, heart rate and steps values from the workout session and calculating the calories from my own formula within the app. But when I save the value of calories by adding the sample in the builder, it shows the different value in the workout section of health app and on clicking its detail it shows the correct value that I saved. What is the issue here? How can I save my own calculated calories value to the workout session recorded in Apple Watch?
let quantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!
let unit = HKUnit.largeCalorie()//HKUnit.kilocalorie()
let caloriesBurned = 100.0
let quantity = HKQuantity(unit: unit, doubleValue: caloriesBurned)
let sample = HKQuantitySample(type: quantityType, quantity: quantity, start: self.workoutStartTime, end: Date())
builder.add([sample]) { (success, error) in
guard success else {
print("error ======> \(error)")
return
}
self.builder.endCollection(withEnd: Date()) { (success, error) in
self.builder.finishWorkout { (workout, error) in
}
}
}
}
I am creating a workout app and when the user creates a workout then a countdown timer is shown. When the watch app is in foreground, then the timer works correct and after 3 seconds the activity screen is shown. But if the Apple Watch sleeps(when I low down the wrist), then after 3 seconds, the new interface controller is not shown. I know that this is because the timer does not run in the background. How does the Apple Watch default workout app handles this? When I create a workout in apple workout app, the Apple Watch does not get to sleep mode during the countdown timer screen. How do they manage this and how can I achieve this in my app so that the user experience is smooth?
I am creating a workout session but sometimes for some of the users, the health monitors do not work like the distance and the heart rate are not fetched. When the workout is finished and after that the activity is started again the health data is fetched correctly. Does anybody has any idea why is this happening and what might be the reason for this. Following is the code of starting an activity:
/// create and begin a workout session for the activity
func startWorkout(activityId: Int, startTime: Date, completion: @escaping (_ success: Bool, _ error: DIError?) -> Void) {
self.activityId = activityId
self.workoutStartTime = startTime
guard let healthStore = HealthKitInterface.sharedInstance?.healthKitDataStore else {
return
}
self.healthStore = healthStore
self.configuration = HKWorkoutConfiguration()
configuration.activityType = self.configActivityType()
configuration.locationType = .outdoor
if self.configActivityType() == .swimming {
configuration.swimmingLocationType = .openWater
}
// Create the session and obtain the workout builder.
do {
if let activeSession = self.activeSession {
session = activeSession
} else {
session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
}
builder = session.associatedWorkoutBuilder()
} catch(let error) {
//FIXME:-dismiss()
let diError = DIError(error: error)
completion(false, diError)
return
}
// Setup session and builder.
session.delegate = self
setBuilderDataSourceDelegate()
// Start the workout session and begin data collection.
//if the session is already started, then we dont need to start activity or begin collection
//if session is not already started
if self.activeSession == nil {
session.startActivity(with: workoutStartTime)
builder.beginCollection(withStart: workoutStartTime) { (success, error) in
}
}
}
I am creating a workout session in Apple Watch. I am tracking the distance and the heart rate through the workout session. The HKLiveWorkoutBuilder is already set. The data collects correctly. But I calculate calories from my own equation. But when I finish the workout, the calories are not saved correctly. For eg. if I have calories from my formula equals to 20kCal, in HealthApp, it Shows a different number.
func endWorkout(caloriesCalc: Double, completion: @escaping (_ success: Bool, _ error: String?) -> Void) {
session.end()
if let quantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned) {
let unit = HKUnit.largeCalorie()//HKUnit.kilocalorie()
let caloriesBurned = caloriesCalc
let quantity = HKQuantity(unit: unit, doubleValue: caloriesBurned)
let sample = HKQuantitySample(type: quantityType, quantity: quantity, start: self.workoutStartTime, end: Date())//HKCumulativeQuantitySample(type: quantityType, quantity: quantity, start: self.workoutStartTime, end: Date(), metadata: nil)
builder.add([sample]) { (success, error) in
guard success else {
print("error ======> \(error)")
return
}
self.builder.endCollection(withEnd: Date()) { (success, error) in
guard success else {
completion(success, "Something went wrong! Please try again.")
return
}
self.activeSession = nil
self.builder.finishWorkout { (workout, error) in
if let error = error {
completion(false, "Something went wrong! Please try again.")
return
}
self.activityType = nil
completion(true, nil)
}
}
}
}
}
In my app, there is already a working auto renewal subscription and now the requirement is to add the 3 months free trial to all the new users and already existing subscribers in the app. Since promotional offers don't work for the new users(non subscribers), so do I need to create introductory offer for new users and one promotional offer for the existing users? Also, If the existing user uses that promotional offer , then what is the way to not show him that promotional offer again in the application UI? How can I test these scenarios through sandbox testers?
How can we assign the unique accessibility identifiers to the urls in UITextView In the accessibility inspector, I am able to see the urls as separate elements but not able to set the accessibility identifiers for them. The accessibility identifiers are needed for automation testing.
In the screenshots added here, i have taken a UITextView which has urls in it.