ios - Core Motion 是否需要授权/.plist 信息?

标签 ios iphone swift cocoa-touch core-motion

我正在尝试在后台使用 CMMotionActivityManagerstartActivityUpdatesToQueue

但它似乎不起作用,除了添加此代码之外,我还需要做其他事情吗:

        if CMMotionActivityManager.isActivityAvailable() {
            CMMotionActivityManager().startActivityUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (activity: CMMotionActivity?) in
                if (activity?.automotive)! {
                    let notification = UILocalNotification()
                    notification.alertTitle = "Arounder"
                    notification.alertBody = "User using car"
                    notification.soundName = "Notification Sound"
                    notification.category = "category"

                    UIApplication.sharedApplication().scheduleLocalNotification(notification)

                    print(notification.alertBody)
                } else if (activity?.cycling)! {
                    let notification = UILocalNotification()
                    notification.alertTitle = "Arounder"
                    notification.alertBody = "User is cycling"
                    notification.soundName = "Notification Sound"
                    notification.category = "category"

                    UIApplication.sharedApplication().scheduleLocalNotification(notification)
                    print(notification.alertBody)
                } else if (activity?.walking)! || (activity?.running)! {
                    let notification = UILocalNotification()
                    notification.alertTitle = "Arounder"
                    notification.alertBody = "User is walking/running"
                    notification.soundName = "Notification Sound"
                    notification.category = "category"

                    UIApplication.sharedApplication().scheduleLocalNotification(notification)
                    print(notification.alertBody)
                } else if (activity?.stationary)! {
                    let notification = UILocalNotification()
                    notification.alertTitle = "Arounder"
                    notification.alertBody = "User is standing"
                    notification.soundName = "Notification Sound"
                    notification.category = "category"

                    UIApplication.sharedApplication().scheduleLocalNotification(notification)
                    print(notification.alertBody)
                } else if (activity?.unknown)! {
                    let notification = UILocalNotification()
                    notification.alertTitle = "Arounder"
                    notification.alertBody = "Unknown activity"
                    notification.soundName = "Notification Sound"
                    notification.category = "category"

                    UIApplication.sharedApplication().scheduleLocalNotification(notification)
                    print(notification.alertBody)
                }
            })
        }

我是否需要像 Core Location (requestAlwaysAuthorization) 中那样获得用户的许可? .plist 文件是什么?

谢谢!

最佳答案

当您的应用处于后台(挂起)时,

CMMotionActivityManager 不会提供更新。来自 documentation :

The handler block is executed on a best effort basis and updates are not delivered while your app is suspended. If updates arrived while your app was suspended, the last update is delivered to your app when it resumes execution. To get all of the updates that occurred while your app was suspended, use the queryActivityStarting(from:to:to:withHandler:) method.

您可以使用其他一些后台模式(例如显着位置更改模式)让您的应用定期恢复后台执行并借此机会检索事件更新,但运动类型本身会发生变化 不会导致您的应用程序从挂起执行到后台执行。

关于ios - Core Motion 是否需要授权/.plist 信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292424/

相关文章:

ios - 碰撞时完全停止 Sprite

iphone - 如何在 iPhone 中将 HTML 读取为 XML

iphone - 更改 UIImageView 模式 iPhone/iPad

swift - 自定义 TableViews + CustomCell

swift - 为什么会出现 URL 错误?

ios - 返回错误的结果用于在 swift 中打印 FIRST 和 LAST DATE OF THE MONTH

ios - Objective-C 运行测试时 : Class defined without specifying a base class

iphone - iPad 分发问题

ios - Swift AnyObject 到 Int 总是以 nil 结束

ios - (Swift) UICollectionViewCell 带有手势识别器的嵌套 subview