ios nstimer executado em segundo plano para verificar o horário do alarme é igual ao horário atual e reproduzir áudio
Pesquisei no Google por horas, mas não consegui encontrar nenhuma informação se houver alguma maneira de manter um NSTimer em execução ativo quando o aplicativo estiver sendo executado em segundo plano? Por favor, ajude-me, quando o aplicativo for para segundo plano, uma função será executada e cada segundo verificará que o horário do alarme é igual ao horário atual; em seguida, reproduza o áudio e abra a página de alarme próximo. Se alguém souber o código aberto do aplicativo de alarme e sua execução em segundo plano, compartilhe comigo Aqui eu tento isso, mas isso não está funcionando
- (void)applicationDidEnterBackground:(UIApplication *)application
{
inBackground=YES;
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (inBackground == YES) {
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(checkAlert) userInfo:nil repeats:YES];
}
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
-(void)checkAlert
{
NSLog(@"Chalaaaaaa");
NSString *checkAlarmOnOff = [defaults stringForKey:@"setAlarm"];
// Switch Alarm From Setting page
NSString *SwitchAlarm=[defaults stringForKey:@"setSwitchAlarm"];
if([SwitchAlarm isEqualToString:@"ON"])
{
if([checkAlarmOnOff isEqualToString:@"YES"])
{
if(![alarmRun isEqualToString:@"Yes"])
{
NSString *getAlarmTime = [defaults stringForKey:@"setAlarmTime"];
NSLog(@"AA-%@",getAlarmTime);
NSLog(@"BB-%@",globalClockTime);
if([getAlarmTime isEqualToString:globalClockTime])
{
[self MusicAction];
[_audioPlayer play];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alarm" message:@"Alarm On" delegate:self cancelButtonTitle:@"Off" otherButtonTitles:nil, nil];
[alert show];
alarmRun=@"Yes";
}
}
}
}
}
Por favor me ajude,