【问题标题】:Does Core Motion requires authorization/.plist info?Core Motion 是否需要授权/.plist 信息?
【发布时间】:2016-10-27 19:09:21
【问题描述】:

我正在尝试在后台使用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 文件是什么东西?

谢谢!

【问题讨论】:

    标签: ios iphone swift cocoa-touch core-motion


    【解决方案1】:

    CMMotionActivityManager 在您的应用处于后台(暂停)时不会提供更新。来自documentation

    处理程序块会尽最大努力执行,并且在您的应用暂停时不会传递更新。如果在您的应用程序暂停时更新到达,则在您的应用程序恢复执行时将最后一次更新传递给您的应用程序。要获取您的应用暂停期间发生的所有更新,请使用 queryActivityStarting(from:to:to:withHandler:) 方法。

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

    【讨论】:

    • 看这个答案,他说你可以在后台获取activity:stackoverflow.com/questions/40238591/…是不是错了?
    • 那个答案和我的基本一样;您需要使用其他一些后台模式,例如位置更新来让您的应用程序执行。然后,您可以利用此后台执行来接收运动活动更新。单独的活动更改不会将您的应用从暂停状态转移到后台执行
    • 哦,所以我要澄清一下:在我的应用程序中,我有一个区域监控,我想在用户到达某个区域时向他发送通知(例如,“家” ) 只要他在走路,所以我调用我在didEnterRegion 方法上添加的代码,可以吗?
    • 应该可以,但您可能需要在didEnterRegion 中使用查询方法developer.apple.com/reference/coremotion/… 来尝试确定最近的活动模式
    • 但是我想要当前用户的活动,而不是从startend
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    相关资源
    最近更新 更多