10
을 구현하기 위해 노력
나는이 내 응용 프로그램이 백그라운드에 들어갈 때 내 AppDelegate
에 다음 코드 :는 beginBackgroundTaskWithExpirationHandler 및 UILocalNotification
var backgroundUpdateTask: UIBackgroundTaskIdentifier!
func beginBackgroundUpdateTask() {
self.backgroundUpdateTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
self.endBackgroundUpdateTask()
})
}
func endBackgroundUpdateTask() {
UIApplication.sharedApplication().endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskInvalid
}
func doBackgroundTask() {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.beginBackgroundUpdateTask()
// Do something with the result.
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "displayAlert", userInfo: nil, repeats: false)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
NSRunLoop.currentRunLoop().run()
// End the background task.
self.endBackgroundUpdateTask()
})
}
func displayAlert() {
let note = UILocalNotification()
note.alertBody = "As a test I'm hoping this will run in the background every X number of seconds..."
note.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(note)
}
func applicationDidEnterBackground(application: UIApplication) {
self.doBackgroundTask()
}
나는 그것이 NSTimer.scheduledTimerWithTimeInterval()
에 지정된 시간 (초)의 UILocalNotification()
마다 X 번호를 실행 바라고을 그러나 한 번만 실행됩니다.
저는 백그라운드 작업이 어떻게 작동하는지 계속 생각하고 있습니다. 내가 빠진 것이 있습니까?
'var backgroundUpdateTask : UIBackgroundTaskIdentifier!'이 (가) 내 코드에서 누락되었습니다. 귀하의 질문에 감사 드리며 퍼즐의 마지막 부분을 발견했습니다. –