-1

ユーザーが特定のボタンをクリックするたびに1分間実行される次のコードがあります。

timer = [[NSTimer alloc]init];
        timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

私の質問は、アプリケーションがバックグラウンド モードの場合にタイマーが動作し続けるようにする方法です。

前もって感謝します!

4

2 に答える 2

3
//NSTimer run in background.

NSTimer *myTimer;  <br>  
 [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];<br>
   myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0
                                 target:self
                               selector:@selector(targetMethod:)
                               userInfo:nil
                                repeats:YES];<br>
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSRunLoopCommonModes];
于 2014-11-12T09:01:47.033 に答える
0

alloc&initタイマーを使用しないでください。

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0
                                 target:self
                               selector:@selector(targetMethod:)
                               userInfo:nil
                                repeats:YES];

好きなところで止めることもできます。

[myTimer invalidate];
于 2013-01-27T17:57:33.447 に答える