0
我试图在后台使用CMMotionActivityManager
的startActivityUpdatesToQueue
。Core 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 Location
(requestAlwaysAuthorization
)从用户获得许可样?有什么东西是.plist
文件?
谢谢!
看看这个答案,他说你可以在后台获取活动:http://stackoverflow.com/questions/40238591/is-it-possible-to-detect-the-users-moving-activity-on背景/ 40239325#40239325这是错的吗? –
答案与我的基本相同;您需要使用其他一些背景模式,例如位置更新来让您的应用程序执行。然后,您可以利用此后台执行来接收动作活动更新。它自己的活动更改不会将您的应用程序从挂起到后台执行 – Paulw11
哦,所以我会澄清一点:在我的应用程序中,我有一些区域监视,我想在用户到达时发送通知给用户一些地区(例如,“家”),就好像他正在走路一样,所以我打电话给我在'didEnterRegion'方法上添加的代码,可以吗? –