ios nstimer run in background for check alarm time is equal to current time and play audio

前端 未结 3 1850
猫巷女王i
猫巷女王i 2020-12-18 17:48

I googled it for hours, but could not find any information if there is any way to keep a running NSTimer active when the app is running in the background ? Please Help me ,

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-18 17:56

    NSTimer will not run in background. and using beginBackgroundTaskWithExpirationHandler won't save your problem. with background task your app can run max 10 min and then will get killed

    you should use UILocalNotification

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = dateTime;
    localNotification.alertBody = [NSString stringWithFormat:@"Alert Fired at %@", dateTime];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    

    calculate time with NSCalendar and add this line:

    localNotification.repeatInterval = NSDayCalendarUnit;
    

提交回复
热议问题