【发布时间】:2016-10-27 19:09:21
【问题描述】:
我正在尝试在后台使用CMMotionActivityManager 的startActivityUpdatesToQueue。
但它似乎不起作用,除了添加此代码之外,我还需要做其他事情吗:
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