iOS 8 HealthKit Santiapps Marcio Valenzuela
iOS 8 HealthKit Santiapps Marcio Valenzuela
iOS 8 HealthKit

Ok so the Journal view controller was quite simple.  Let’s take another quick break by looking at an even simpler view controller, the FoodPicker:

import Foundation

import UIKit

import HealthKit

class FoodPickerViewController: UITableViewController {

var selectedFoodItem: FoodItem?

let FoodPickerViewControllerTableViewCellIdentifier:NSString = "cell"

let FoodPickerViewControllerUnwindSegueIdentifier:NSString = "FoodPickerViewControllerUnwindSegueIdentifier"

var foodItems: NSArray = NSArray()

var energyFormatter: NSEnergyFormatter {

var energyFormatter: NSEnergyFormatter?

var onceToken: dispatch_once_t = 0

dispatch_once(&onceToken, {

energyFormatter = NSEnergyFormatter()

energyFormatter?.unitStyle = NSFormattingUnitStyle.Long

energyFormatter?.forFoodEnergyUse = true

energyFormatter?.numberFormatter.maximumFractionDigits = 2

})

return energyFormatter!

}

var  healthStore:HKHealthStore?

}

Here we are looking at imports, subclassing UITableViewController and property declarations.

Our first property is selectedFoodItem, which will be set to whatever value the user selects from this tableview as an item that he or she consumed.  Then we have 2 identifiers, one for our tableviewcell and another for our unwind segue.  Remember to set both in the storyboard.  To set the cell identifier you must select the cell in the tableview and for the unwind segue you must select the segue from the “Add” button in Journal to the FoodPicker.  Then we create an array to store the hard coded values of our FoodItems.  Ideally you might want to pull this data from a web service.

Notice we use the NSEnergyFormatter here again.  You could also just create a class for this and access it globally.  Finally we add our health store variable.

Now let’s look at our viewDidLoad method:

override func viewDidLoad() {

super.viewDidLoad()

self.foodItems = [FoodItem(name: "Wheat Bagel", joules:240000.0),

FoodItem(name: "Bran with Raisins", joules:190000.0),

FoodItem(name: "Regular Instant Coffee", joules:1000.0),

FoodItem(name: "Banana", joules:439320.0),

FoodItem(name: "Cranberry Bagel", joules:416000.0),

FoodItem(name: "Oatmeal", joules:150000.0),

FoodItem(name: "Fruits Salad", joules:60000.0),

FoodItem(name: "Fried Sea Bass", joules:200000.0),

FoodItem(name: "Chips", joules:190000.0),

FoodItem(name: "Chicken Taco", joules:170000.0) ]

}

Nothing special here, just hardcoding some FoodItems into our array.  So now our tableview methods:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return self.foodItems.count

}

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {

let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier(FoodPickerViewControllerTableViewCellIdentifier, forIndexPath:indexPath!) as UITableViewCell

var foodItem: FoodItem = self.foodItems[indexPath!.row] as FoodItem

cell.textLabel.text = foodItem.name

cell.detailTextLabel?.text = energyFormatter.stringFromJoules(foodItem.joules)

return cell;

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

NSLog("performing unwind segue from FOODPICKER!")

if (segue.identifier == FoodPickerViewControllerUnwindSegueIdentifier) {

var indexPathForSelectedRow: NSIndexPath = self.tableView.indexPathForSelectedRow()!

self.selectedFoodItem = self.foodItems[indexPathForSelectedRow.row] as? FoodItem

}

}

There, now that’s a nice breather.   We set our rows according to the number of items in the array, set our cell values in the same way we did before.  And in our prepareForSegue method, we check our identifier and then set our own selectedFoodItem property from whatever the user selected.

Ok so you’ve had 2 easy classes to rest.  Now let’s get into the longest one, Part 5.

One Comment

  • […] As for the segue, this is the initiating view controller, so we need an unwind because when the user finishes selecting an item in the FoodPicker, we must do some stuff.  What stuff?  Well first we get our sourceViewController for the “unwind” segue.  Don’t get confused because normally you get the destination segue of a forward segue in order to set that destination view controller’s properties.  In this case, we are in the calling view controller and we are unwinding from a segue.  Our source view controller is FoodPicker and our destination is Journal.  We then take the source’s selectedFoodItem property and set it as a local variable called selectedFoodItem here, locally, in Journal.  Then we call our addFoodItem() method passing it in that local variable selectedFoodItem.  That saves that food item’s name in metadata and its joules into the health store as DietaryConsumedEnergy. Well, that was quite simple.  The FoodPicker is actually quite simple.  See you there (Part 4)! […]

Leave a Reply