我试图在我的视图出现之前调用HealthKit授权函数,但它在视图加载后不断出现,这不是我想要的。我也得到了这个错误:
对开始/结束外观转换的不平衡调用
(我假设错误告诉我,我所做的事情是错的,我知道。)
我应该如何调用此函数以使HealthKit授权对话框在加载之前覆盖视图控制器的视图?
class SummaryViewController: UIViewController {
//MARK: Properties
@IBOutlet weak var chart: CounterView!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var pieChart: CounterView!
@IBOutlet weak var dateLabel: UILabel!
var healthData = HealthData()
let counterView = CounterView()
var scoreInt = 0
let yesterday = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -1, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))
func formatDate(date:NSDate) -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
let dateString = dateFormatter.stringFromDate(date)
return dateString
}
override func viewWillAppear(animated: Bool) {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
func healthKitAuth() {
HealthData().authorizeHealthKit { (authorized, error) -> Void in
if authorized {
print("HealthKit authorization received.")
self.healthData.updateHealthCharacteristics()
let seconds = 0.1
let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
self.scoreInt = Int(self.healthData.score)
self.scoreLabel.text = String(self.scoreInt)
self.counterView.counter = self.scoreInt
self.pieChart.counter = self.counterView.counter
let dateString = self.formatDate(self.yesterday!)
self.dateLabel.text = String(dateString)
})
}
else
{
print("HealthKit authorization denied!")
if error != nil {
print("\(error)")
}
}
}
}
healthKitAuth()
}
}
答案 0 :(得分:1)
您收到的警告是因为您在转换过程中调用了转换。您不应尝试在viewWillAppear
方法中显示视图控制器。 (authorizeHealthKit
函数提供了一个视图控制器......)
至于你想要的......你需要在加载视图控制器之前调用authorizeHealthKit
函数。要做到这一点,您可能需要创建一个新的视图控制器,除了检查HealthKit授权然后加载视图控制器之外什么都不做。