我当前正在构建一个应用程序,您可以在其中阅读最近7天的HeartRate。一旦您请求数据,它就会显示在UITableView
中,无论何时加载数据,您都可以通过电子邮件按钮发送结果。然后在电子邮件正文中填满所有DataHeart。我希望只获取时间和心跳率,但是我没有办法清楚地解析它。同样,每当我按下“提交”或“取消”按钮时,我都无法返回上一屏幕。该应用程序不会崩溃或出现任何错误,它只会让我返回。
这是我的代码块:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getHealthKitPermission()
}
let healthStore = HKHealthStore()
var heartRateData: [HKSample]?
@IBOutlet weak var tblHeartRateData: UITableView!
@IBAction func getHeartRate(_ sender: Any) {
getTodaysHeartRate { (result) in
DispatchQueue.main.async {
self.heartRateData = result
self.tblHeartRateData.reloadData()
}
}
}
@IBAction func emailResult(_ sender: Any) {
self.sendEmail()
}
func getTodaysHeartRate(completion: (@escaping ([HKSample]) -> Void)) {
print("func")
let heartRateType:HKQuantityType = HKQuantityType.quantityType(forIdentifier: .heartRate)!
let startDate = Date() - 7 * 24 * 60 * 60
let endDate = Date()
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])
let sortDescriptors = [
NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
]
let heartRateQuery = HKSampleQuery(sampleType: heartRateType,
predicate: predicate,
limit: Int(HKObjectQueryNoLimit),
sortDescriptors: sortDescriptors)
{ (query:HKSampleQuery, results:[HKSample]?, error:Error?) -> Void in
guard error == nil else { print("error"); return }
completion(results!)
}
healthStore.execute(heartRateQuery)
}
func sendEmail() {
if let currentHeartRate = heartRateData{
if MFMailComposeViewController.canSendMail() {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
let result = formatter.string(from: date)
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self as? MFMailComposeViewControllerDelegate
mail.setToRecipients([""])
mail.setMessageBody("<p>Your Heart Rate is: \(currentHeartRate)<p>" + result, isHTML: true)
present(mail, animated: true)
} else {
//Do nothing
}
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return heartRateData?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HeartDataTableViewCell
let heartRateUnit:HKUnit = HKUnit(from: "count/min")
let heartData = self.heartRateData?[indexPath.row]
guard let currData:HKQuantitySample = heartData as? HKQuantitySample else { return UITableViewCell()}
cell.labelTimes.text = "Times: \(startDateSring)"
cell.labelHeartRate.text = "Heart Rate: \(currData.quantity.doubleValue(for: heartRateUnit))"
let startDateFormatter = DateFormatter()
startDateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
startDateFormatter.timeZone = TimeZone.current
let startDateSring = startDateFormatter.string(from: currData.startDate)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return
}
}
答案 0 :(得分:2)
您的电子邮件发送功能应如下所示:
Character
请注意,您需要符合邮件撰写委托,例如:
Character