如何在后台运行NSTimer并在iOS中睡觉?

Fir*_*ist 3 cocoa-touch nstimer ios

我在stackoverflow中发现了很多关于NSTimer在后台运行的帖子.

但是我找不到任何解决方案.

在我的应用程序中,我在后台播放声音,并设置计时器以在达到该时间时停止音乐.

所以我需要运行我的NSTimer背景(意味着当主页按钮点击并睡眠iPhone).

我怎样才能做到这一点?

vua*_*thu 29

// NSTimer run when app in background <br>

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
loop = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(Update) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:loop forMode:NSRunLoopCommonModes];
Run Code Online (Sandbox Code Playgroud)

这就是你想要的?

  • 这会运行多长时间? (3认同)