How to keep NSTimer alive in Background on iOS 7?

后端 未结 4 736
情书的邮戳
情书的邮戳 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;   
    }
    

提交回复
热议问题