iOS 8 HealthKit Santiapps Marcio Valenzuela

Saving HealthKit Data & Closures

This code bit saves: healthKitStore.saveObject(bmiSample, withCompletion: { (success, error) -> Void in if( error != nil ) { println(“Error saving BMI sample: \(error.localizedDescription)”) } else { println(“BMI sample saved successfully!”) } }) The method signature is: saveObject(object: HKObject!, withCompletion completion: ((Bool, NSError!) -> Void)!) This method takes an HKObject which is bmiSample and it takes a completion closure which itself takes a bool & error and returns void. So in our method call, we pass in the bmiSample as the HKObject and for the success and error completion block we say: if error is NOT nil then log that error’s description, else log that the bmiSample was saved successfully.  … Read More

Continue Reading

HealthKit for iOS8: Part 7

4. CoreData for other non-health stats You made it to the end!  Ok, so we are basically going to be adding another store to our app and reading and writing data to THAT store as well. First let’s add a new tab and make it a UITableViewController as well.  It will have dynamically populated cells. Now embed it!   Your final storyboard should look like this:   Add a new Swift class called Swimming Data and set that new UITableViewController scene to its class.  Make that class file look like this: import Foundation import UIKit class SwimmingData: UITableViewController { } Now we must add CoreData.  To do this we need to… Read More

Continue Reading

HealthKit for iOS8: Part 6

Here is the start of our WorkOut view controller: import Foundation import UIKit import HealthKit class WorkoutViewController: UITableViewController, UITextFieldDelegate { @IBOutlet var numberOfLapsTextField: UITextField! @IBOutlet var metersPerLapTextField: UITextField! @IBOutlet var workoutDurationTextField: UITextField! @IBOutlet var paceTextField: UITextField! var numberOfLapsValue: NSString? var metersPerLapValue: NSString? var workoutDurationValue: NSString? var userWeight: Double? var  healthStore:HKHealthStore? } We import what we need, we subclass UITableViewController and add the Text Field delegate protocol.  Here I have created 4 labels for: numberOfLaps metersPerLap workoutDuration pace These labels have an underlying variable for each.  The reason the first 3 are strings is because these are not health kit data per se.  These will be stored in CoreData.  However they… Read More

Continue Reading

iOS : Swift : Blocks = Closures

I’ve never really liked blocks in ObjC. When Swift came out it made things more complicated for me because I’ve never really liked C either. Finally when I had to deal with closures in Swift, well that’s just gonna piss a lot of people off! After a few days reviewing tons of material online, and I mean TONS!  I came to understand this: The only C-like exposure I had prior to ObjC was a little PHP.  So that allows me to understand a function, which is the equivalent of a method in ObjC: DECLARING func sayHello( ) {      println(“Hello World”) } CALLING sayHello( ) RESULT Hello World Even… Read More

Continue Reading