即使我的應用程序在後臺運行,我也想繼續在後臺執行特定的任務。保持NSTimer運行甚至應用程序在ios中的後臺運行
這是我試過的代碼。定時器僅在進入背景時觸發一次。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSTimer * timer = [NSTimer timerWithTimeInterval:2.0
target:self
selector:@selector(timerTicked)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer
forMode:NSDefaultRunLoopMode];
}
- (void) timerTicked
{
NSLog(@"Timer method");
}
嘿,這篇文章似乎是你的問題的解決方案。 http://stackoverflow.com/questions/12916633/how-to-run-the-timer-in-background-of-the-application – LoVo 2015-03-03 10:20:57
@LoVo你解決了我的問題。謝謝.. – Rakesh 2015-03-03 10:33:43
@Rakesh不,它不會。看看這種方法的應用程序文檔。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler: 你在做什麼是反對蘋果推薦,這贏了即使工作超過幾分鐘 – KIDdAe 2015-03-03 13:29:04