有没有可靠的方法来检测应用程序是否已在预热模式下启动?有一些参考文献here。但这对我们来说还无法预测。
还有其他方法吗?
目前只能尝试以下代码:
BOOL isApplicationPreWarmed()
{
return [NSProcessInfo.processInfo.environment objectForKey:@"ActivePrewarm"] != nil;
}
您找不到它的官方 API,但是,Sentry iOS SDK 使用以下代码:
+ (void)load
{
// Invoked whenever this class is added to the Objective-C runtime.
runtimeInit = [NSDate date];
isActivePrewarm =
[[NSProcessInfo processInfo].environment[@"ActivePrewarm"] isEqualToString:@"1"];
}
如果应用程序启动已预热,操作系统会设置环境变量
ActivePrewarm
。然而,这个变量是
在UIApplicationDidFinishLaunchingNotification
之后被删除,所以Sentry在load
之前执行的UIApplicationDidFinishLaunchingNotification
方法中检查这个变量,并使用全局变量isActivePrewarm
来记录它。