在 iOS 上识别预热的可靠方法

问题描述 投票:0回答:2

有没有可靠的方法来检测应用程序是否已在预热模式下启动?有一些参考文献here。但这对我们来说还无法预测。

还有其他方法吗?

ios ios-prewarming
2个回答
0
投票

目前只能尝试以下代码:

BOOL isApplicationPreWarmed()
{
    return [NSProcessInfo.processInfo.environment objectForKey:@"ActivePrewarm"] != nil;
}

0
投票

您找不到它的官方 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
来记录它。

© www.soinside.com 2019 - 2024. All rights reserved.