HealthKit (iOS) won't deliver data in background (objC)
I see something that might be causing an issue in your AppDelegate
, particularly this line:
[[NSURLConnection alloc] initWithRequest:request delegate:self];
This is creating an NSURLConnection, but not starting it. Try changing it to this:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
Edit: After taking a second look at the docs
They recommend setting up your observer queries in your application didFinishLaunchingWithOptions:
method. In your code above, you set the HKObserverQuery
up in the authorization handler, which is called on a random background queue. Try making this change to set it up on the main thread:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setTypes];
[self observeQuantityType];
return YES;
}
HKObserverQuery Reference
Checking healthkit data periodically in background swift
The HealthKit database is encrypted when the device is locked. This means you are not able to read any data from it at all when the device requires you to enter your passcode/fingerprint (which the majority of devices have enabled). So unfortunately, even if you are able to get something to run in the background every minute you would not be able to read the data (your query will just return an error instead of any results).
However, step data can still be accessed from the pedometer (this data is not encrypted). I recommend you look at using that instead of HealthKit for any background processing.
Now it sounds like you really only need to check if the user has taken steps in the last hour. It would be much more efficient if you would only check that often instead of every minute. If your app has access to a server with push notifications setup, you could schedule a silent push notification to wake up your app in the background and do the step check from the pedometer once an hour.
Related Topics
Set the Center of a Uibutton Programmatically - Swift
How to Detect Which Annotation Was Selected in Mapview
How to Handle Homophones in Speech Recognition
How to Simultaneously Satisfy Constraints with Keyboard and Uitoolbar
How to Access Index of Tabbar Item Using Swift
My App Is Rejected with Exception Exc_Breakpoint (Sigtrap)
How to Make a Uiimage Scrollable Inside a Uiscrollview
Uiimage Aspect Fill When Using Drawinrect
Embedding a Uitableview as a Container View Within a Uiviewcontroller
App Crashes When User Starts Typing in Uisearchbar
Reload View When @Published Is Changed