CoreMotion似乎没有授予计步器访问权限

时间:2018-10-11 03:18:32

标签: ios swift xcode core-motion

我最近尝试在我的应用中实现CoreMotion并显示用户已采取的步骤数。我提供的代码可以正常运行,但是当我在print(CMPedometer.isStepCountingAvailable方法中运行下面的代码行viewDidLoad时,调试日志将输出false,而stepCountLabel则显示“ Not可用”(如果计步器不可用则应如此)。我已经在我的info.plist中添加了Privacy - Motion Usage Description,以询问用户是否有权访问CoreMotion数据,但仍然无法访问该数据。如何更改CMPedometer.isStepCountingAvailable以打印true并实际访问此数据?

计步器视图控制器代码:

import UIKit
import CoreMotion

class PedometerController: UIViewController {

@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var stepCountLabel: UILabel!

private let pedometer = CMPedometer()
private let activityManager = CMMotionActivityManager()
private var shouldStartUpdating: Bool = false
private var startDate: Date? = nil


override func viewDidLoad() {
    super.viewDidLoad()
    startButton.addTarget(self, action: #selector(didTapStartButton), for: .touchUpInside)
    print(CMPedometer.isStepCountingAvailable())
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    guard let startDate = startDate else { return }
    updateStepsCountLabelUsing(startDate: startDate)
    print(startDate)
}

@objc private func didTapStartButton() {
    shouldStartUpdating = !shouldStartUpdating
    shouldStartUpdating ? (onStart()) : (onStop())
   }
}

 extension PedometerController {
private func onStart() {
    startButton.setTitle("Stop", for: .normal)
    startDate = Date()
    checkAuthorizationStatus()
    startUpdating()
}

private func onStop() {
    startButton.setTitle("Start", for: .normal)
    startDate = nil
    stopUpdating()
}

private func startUpdating() {

    if CMPedometer.isStepCountingAvailable() {
        startCountingSteps()
    } else {
        stepCountLabel.text = "Not available"
    }
}

private func checkAuthorizationStatus() {
    switch CMMotionActivityManager.authorizationStatus() {
    case CMAuthorizationStatus.denied:
        onStop()
        //activityTypeLabel.text = "Not available"
        stepCountLabel.text = "Not available"
    default:break
    }
}

private func stopUpdating() {
    activityManager.stopActivityUpdates()
    pedometer.stopUpdates()
    pedometer.stopEventUpdates()
}

private func on(error: Error) {
    //handle error
}

private func updateStepsCountLabelUsing(startDate: Date) {
    pedometer.queryPedometerData(from: startDate, to: Date()) {
        [weak self] pedometerData, error in
        if let error = error {
            self?.on(error: error)
        } else if let pedometerData = pedometerData {
            DispatchQueue.main.async {
                self?.stepCountLabel.text = String(describing: pedometerData.numberOfSteps)
            }
        }
    }
}

private func startCountingSteps() {
    pedometer.startUpdates(from: Date()) {
        [weak self] pedometerData, error in
        guard let pedometerData = pedometerData, error == nil else { return }

        //DispatchQueue.main.async {
            self?.stepCountLabel.text = pedometerData.numberOfSteps.stringValue
            print(pedometerData.startDate)
            print("Boss")
        //}
    }
}


}

0 个答案:

没有答案