iOS 代码覆盖率 XcodeCoverage 真机

简记XcodeCoverage,

1,Podfile文件: pod 'XcodeCoverage', '~>1.0'
2,pod install
3,Xcode - Build Phases - TARGETS - Run Script,设置:
Pods/XcodeCoverage/exportenv.sh
4,Xcode - Build Settings - Generate Legacy Test Coverage Files - Yes (仅设置Debug即可)
5,Xcode - Build Settings - Instrument Program Flow - Yes (仅设置Debug即可)
6,AppDelegate.m: (触发点是点击 home 键应用进入后台时)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)applicationDidEnterBackground:(UIApplication *)application
{
...
#if !TARGET_IPHONE_SIMULATOR
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
setenv("GCOV_PREFIX", [documentsDirectory cStringUsingEncoding:NSUTF8StringEncoding], 1);
setenv("GCOV_PREFIX_STRIP", "13", 1);
#endif

extern void __gcov_flush(void);
__gcov_flush();
...
}

7,Xcode - Build - Run… - (Play)… - 按home键,app进入后台
8,Xcode - Window - Devices - 目标设备 - Installed Apps - 目标app - 底部齿轮按钮 - Download Container - 下载下来的xcappdata 文件右键 show package content - 获取 AppData/Documents/arm64/ 下所有 .gcda 文件
9,项目/Pods/XcodeCoverage - env.sh - 获取 OBJECT_FILE_DIR_normal 和 CURRENT_ARCH - copy上一步设备上得到的 .gcda 文件到 <OBJECT_FILE_DIR_normal>/<CURRENT_ARCH> 路径下。
10,项目/Pods/XcodeCoverage下运行./getcov --show


参考资料: