簡體   English   中英

iOS后台任務計時器

[英]ios background task timer

我正在設計一個需要在特定時間在后台模式下將數據發送到服務器的應用程序。 所以我想知道:在我安排一次應用程序之后(一次),應用程序是否有可能在24小時內執行一段代碼?

非常感謝!

將日期存儲在NSUserDefaults然后進一步檢查24小時..

  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"];

}

希望對你有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM