iOS 小技巧总结

1.获取准确的app启动所需时间

应用启动时间长短对用户第一次体验至关重要,同时系统对应用的启动、恢复等状态的运行时间也有严格要求,在应用超时的情况下系统会直接关闭应用。
以下是几个常见场景下系统对App运行时间的要求:

Launch 20秒
Resume 10秒
Suspend 10秒
Quit 6秒
Background Task 10分钟

1)在点main.m文件中加入

CFAbsoluteTime StartTime;//开始时间
int main(int argc, char * argv[]) {
    @autoreleasepool {
         StartTime=CFAbsoluteTimeGetCurrent();//开始时间
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

2)AppDelegate.m文件中加入

@interface AppDelegate ()

/**
 *  声明startTime,用以在本类中调用main.m中的startTime全局变量。
 */
extern CFAbsoluteTime StartTime;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];
    
     NSLog(@"程序启动耗时%f秒!",CFAbsoluteTimeGetCurrent()-StartTime);//时间差

    return YES;
}
 
 
 
原文地址:https://www.cnblogs.com/sixindev/p/4466482.html