Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 每天午夜更新图标徽章_Ios_Auto Update - Fatal编程技术网

Ios 每天午夜更新图标徽章

Ios 每天午夜更新图标徽章,ios,auto-update,Ios,Auto Update,我正在制作一个应用程序,显示两个人恋爱后的天数。我想在应用图标徽章上显示天数。我知道在用户离开应用程序后如何执行此操作,但是我想每天更新图标徽章,即使应用程序未打开或未在后台运行,这样用户就可以知道无需打开应用程序的天数。“BeenTogether”是一个类似的应用程序,它也在做同样的事情,所以我相信这是可能的。关于如何实现这一点,你有什么想法吗?我敢肯定,关于这一点的各种说法已经被问了无数次,但答案仍然是否定的。你可以用一些方法来近似它,但都有缺点 静默推送通知。当然,这意味着你需要知道他们

我正在制作一个应用程序,显示两个人恋爱后的天数。我想在应用图标徽章上显示天数。我知道在用户离开应用程序后如何执行此操作,但是我想每天更新图标徽章,即使应用程序未打开或未在后台运行,这样用户就可以知道无需打开应用程序的天数。“BeenTogether”是一个类似的应用程序,它也在做同样的事情,所以我相信这是可能的。关于如何实现这一点,你有什么想法吗?

我敢肯定,关于这一点的各种说法已经被问了无数次,但答案仍然是否定的。你可以用一些方法来近似它,但都有缺点

  • 静默推送通知。当然,这意味着你需要知道他们的时区,他们需要有一个网络连接
  • 后台获取。这是“定期”操作的,所以你不能在午夜的时候更新徽章,但它通常非常接近

    • 通过本地通知,您可以实现同样的效果。我想他们开始约会的时候你已经有约会了。因此,您可以通过本地通知每天更新徽章计数

      我不确定每个人都理解我的问题,因为事实证明,每天午夜可以将图标徽章增加一个,而不必安排64次通知。解决方案相当简单:

      //Set a random date (only the time matters because it is repeated everyday), but make sure that the time is at midnight!!
      NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
      [dateComponents setYear:2015];
      [dateComponents setMonth:1];
      [dateComponents setDay:1];
      [dateComponents setHour:0];
      [dateComponents setMinute:0];
      
      //Create an NSDate from the date components
      NSCalendar *calendar = [[NSCalendar alloc]  initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
      NSDate *configuredDate = [calendar dateFromComponents:dateComponents];
      
      //Schedule a local notification, set the repeatInterval to daily
      UILocalNotification* localNotification = [[UILocalNotification alloc] init];
      localNotification.fireDate = configuredDate;
      localNotification.alertBody = nil;
      localNotification.alertAction = nil;
      localNotification.timeZone = [NSTimeZone defaultTimeZone];
      localNotification.repeatInterval = NSCalendarUnitDay;
      
      //Add one to the icon badge number
      localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
      
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
      

      Stephen,我听说过推送通知方法,但是对于这样一个简单的任务来说,它似乎相当复杂。虽然后台抓取看起来像是一个实际的解决方案,但即使应用程序在后台关闭,它也能工作吗?谢谢是的,如果应用程序未运行,它将启动应用程序(在后台)。然而,它不是100%可靠。你能给我一些代码,我将如何做到这一点?你可以安排通知在每24小时与当前日期。iOS允许安排最多64个通知。所以,你需要写相应的逻辑。好主意。问题是您必须运行才能创建/编辑通知。你会提前创建一些通知,并希望用户再次启动以重新生成它们吗?哦,是的,我听说过这种方法,但我试图避免这种情况,如果可能的话,因为如果应用程序在64天内没有打开,它将停止更新。你可以重复通知,但如何增加徽章号?