Topics

#36: Today View Widgets 📟

Topics

Creating a Today View Widget for your app is a great way to give users a quick way to access timely information or controls. Let's make one!

Add the Widget Target

Implement Regular Table View Controller Stuff

class TodayViewController: UITableViewController, NCWidgetProviding {
  var bites = [Bite]()

  // not shown here because boring:
  // numberOfRowsInSection returns bites.count
  // cellForRowAtIndexPath just sets cell.textLabel.text = bite.title
}

Conform to NCWidgetProviding Protocol

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
  Bite.fetchLatest { (bites, error) in
    guard error == nil else { completionHandler(.Failed); return }
    self.bites = bites

      self.tableView?.reloadData()
      self.preferredContentSize = self.tableView!.contentSize

    completionHandler(.NewData)
  }
}

Success!