How to keep NSTimer alive in Background on iOS 7?

后端 未结 4 601
情书的邮戳
情书的邮戳 2021-01-01 03:35

I have created Application which runs NSTimer in Background. I used the Location manager to run the NSTimer in background,

I used below link to run NSTimer in backgr

相关标签:
4条回答
  • 2021-01-01 03:51
    -(void)start {
    
    [[NSUserDefaults standardUserDefaults ]setObject:[NSDate date] forKey:@"startTimer"];
    
     Nstimer*   timer2=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
    }
    -(void)timerFired
    {
          @try {
        NSDate *timerStartDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"startTimer"];
    
    
    NSTimeInterval interval=[timerStartDate timeIntervalSinceNow];
    
    int hour1=-interval/3600;
    
    int rem =((int)interval)%3600 ;
    
    int min1 = -rem/60 ;
    
    int sec1 = -rem %60 ;
    
    // NSLog(@"hour %i  rem %i",hour,rem);    
    // NSLog(@"hour%i",hour1);
    // NSLog(@"min%i",min1);
    // NSLog(@"sec%i",sec1);
    
    
    NSString *strmin=[NSString stringWithFormat:@"%i",min1];
    NSString *strhour=[NSString stringWithFormat:@"%i",hour1];
    
    if ([strmin integerValue]<10)
    {
        [lblSeconds setText:[NSString stringWithFormat:@"0%@",strmin]];
    }
    else 
    {
        lblSeconds.text=strmin;
    
    }
    if ([strhour integerValue]<10) {
        [lblHour setText:[NSString stringWithFormat:@"0%@",strhour]];
    }
    else
    {
        lblHour.text=strhour;
    
    }
    
    }
    @catch (NSException *exception) {
        NSLog(@"exception in timer %@ ",[exception description]);
    
    }
      return;   
    }
    
    0 讨论(0)
  • 2021-01-01 04:10

    In iOS7, there is a new mode for periodic data fetch. Add the fetch background mode to your app, and in your application delegate, pass an interval to - [UIApplication setMinimumBackgroundFetchInterval:. Your app's delegate will start receiving calls to application:performFetchWithCompletionHandler: once the app is in the background.

    More information here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:performFetchWithCompletionHandler:

    0 讨论(0)
  • 2021-01-01 04:12
    NSTimer *currentCycleTimer;
    
    UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
    UIApplication *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    }];
    [currentCycleTimer invalidate];
    currentCycleTimer=nil;
    secondsLeft = 120;
    currentCycleTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self        selector:@selector(Countdown) userInfo:nil repeats: YES];
    
    -(void) Countdown
    {
      [currentCycleTimer invalidate];
      currentCycleTimer=nil;
    }
    
    0 讨论(0)
  • 2021-01-01 04:13
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
    loop = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(Update) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:loop forMode:NSRunLoopCommonModes];
    
    0 讨论(0)
提交回复
热议问题