5分ごとに特定の機能を実行できるiOSアプリを作りたいです。どうやってするの?関数は次のように単純です
batter = 95; // will be calculated.. every time is differ
[UIApplication sharedApplication].applicationIconBadgeNumber = batter;
バックグラウンドでタイマーを作成する必要があります
アプリが VOIP、音声再生、または位置情報の更新のいずれかのカテゴリに分類されない限り、バックグラウンドでこれを行うことはできません。タイマーを配置すると、アプリケーションがバックグラウンドに移行したときにタイマーが無効になります。
.h ファイルでタイマーを次のように宣言します。
NSTimer *MyTimer;
@property (nonatomic, retain) NSTimer *MyTimer;
次に、.m ファイルで、次のように宣言しました。
UIBackgroundTaskIdentifier BackGroundTask;
UIApplication *app = [UIApplication sharedApplication];
BackGroundTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:BackGroundTask];
}];
self.MyTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self
selector:@selector(TimerMethod) userInfo:nil repeats:YES];
以下は、5 秒ごとに呼び出すタイマー メソッドです。
-(void)TimerMethod
{
batter = 95; // will be calculated.. every time is differ
[UIApplication sharedApplication].applicationIconBadgeNumber = batter;
}