简体   繁体   中英

ios background task timer

I'm designing an app requiring sending data to server at a certain time in background mode. So I'm wondering: Is it possible for an app to execute a piece of code 24h after I scheduled it(just once)?

Thanks a lot!

store date in NSUserDefaults and further you check for 24 hours ..

  NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:@"CONSCIENCE_START_DATE"];

if (!date) {

    // This is the 1st run of the app

    date = [NSDate date];

    [[NSUserDefaults standardUserDefaults] setObject:date forKey:@"CONSCIENCE_START_DATE"];

}

NSString *str = [NSString stringWithFormat:@"%@",date];

NSLog(@"  date string = %@",str);

NSTimeInterval secondsBetween = [[NSDate date] timeIntervalSinceDate:date]; // in seconds

int numberOfHours = secondsBetween / 3600; // convert in hours

NSLog(@"There are %d hours in between the two dates.", numberOfHours);

if (numberOfHours > 24) {

    // do your code here ...

    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"CONSCIENCE_START_DATE"];

}

hope it help you .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM