2016-10-27 15 views
0

我试图在后台使用CMMotionActivityManagerstartActivityUpdatesToQueueCore Motion是否需要授权/ .plist信息?

但它似乎没有工作,有没有我需要的,除了添加此代码做更多的事情:

 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 LocationrequestAlwaysAuthorization)从用户获得许可样?有什么东西是.plist文件?

谢谢!

回答

0

CMMotionActivityManager在您的应用处于后台(暂停)时不会传送更新。从documentation

处理程序块在尽力而为的基础上执行并且更新不会在您的应用程序暂停时传递。如果更新在应用程序暂停时到达,则最新更新会在恢复执行时交付给您的应用程序。要获取应用程序挂起期间发生的所有更新,请使用queryActivityStarting(from:to:to:withHandler :)方法。

你可以使用一些其他的背景模式,如显著位置的变化模式,让自己的应用周期性恢复的背景执行,并借此机会来检索活动更新,但在运动型改变自己的会不会导致您的应用从暂停状态转移到后台执行状态。

+0

看看这个答案,他说你可以在后台获取活动:http://stackoverflow.com/questions/40238591/is-it-possible-to-detect-the-users-moving-activity-on背景/ 40239325#40239325这是错的吗? –

+0

答案与我的基本相同;您需要使用其他一些背景模式,例如位置更新来让您的应用程序执行。然后,您可以利用此后台执行来接收动作活动更新。它自己的活动更改不会将您的应用程序从挂起到后台执行 – Paulw11

+0

哦,所以我会澄清一点:在我的应用程序中,我有一些区域监视,我想在用户到达时发送通知给用户一些地区(例如,“家”),就好像他正在走路一样,所以我打电话给我在'didEnterRegion'方法上添加的代码,可以吗? –