我正在研究一种温和的反作弊解决方案,以抵消设备时间的变化。目标是通过使用设备正常运行时间来确定当前 DateTime。当设备重新启动时,该值应与设备报告的任何内容同步。
它将组合成一个分层结构,首先使用互联网时间(如果可用),然后是正常运行时间,最后默认为设备时间。
我所缺少的只是一种检测设备是否在应用 session 之间重新启动的可靠方法。
我试过:
- 从内核获取引导时间。这不是一个选项,因为启动时间会在设备时间更改时更改。
- 在
NSTemporaryDirectory()
中创建一个文件,并在应用程序启动时检查它是否存在。不幸的是,该文件至少会保留 1 周,然后才会在重新启动时被删除。 - 比较正常运行时间。在以下情况下,这可能会导致误报:
14:00 启动应用程序,正常运行时间为 20 分钟 => 我确定启动时间为 13:40。
在 15:00 启动应用程序,正常运行时间为 25 分钟 => 我无法确定用户是否在 14:35 重新启动了设备,或者是否在没有重新启动设备的情况下更改了时间。
我能够通过读取位于 /proc/sys/kernel/random/boot_id
的文件的内容来检测 Android 上的重启。它会在每次启动时填充一个随机生成的 ID。我在 iOS 上找不到类似的东西。
我使用此代码检索正常运行时间/启动时间:
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
time_t now;
time_t uptime = -1;
(void)time(&now);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0)
{
uptime = now - boottime.tv_sec;
}
return uptime;
最佳答案
Comparing uptime. This can result in a false positive in the following scenario: Started the app at 14:00, with an uptime of 20min => I determine the boottime to be 13:40. Started the app at 15:00, with an uptime of 25min => I am unable to determine whether the user rebooted the device at 14:35, or if the time was changed without rebooting the device.
如果您不相信从系统中获取的时间,请改为从外部来源获取。参见 Get Date and Time from Apple Server有关为此使用网络时间协议(protocol)的建议。然后,您可以使用 uptime
计算启动时间,并将其与之前计算和存储的启动时间进行比较。
关于ios - 如何可靠地检测到 iOS 设备已重启?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58822703/