iOS分析

页面响应

页面响应采用[[检测iOS的APP性能的一些方法]]里的子线程监控主线程方式就能够检测到。

电量

三种方式可以获取电量,分别是通过Instruments的Energy Diagnostics,UIDevice和IOKit framework

Instruments的Energy Diagnostics

可以获取到iphone特定时段的电量消耗信息。具体步骤:
打开Developer选项中的Start Logging —> 断开iphone与PC连接 —> 一系列的用户操作 —> Stop Logging —> 连接iphone与PC, 将电量消耗数据导入Instruments。 

UIDevice

UIDevice提供了设备的详细信息,如systemVersion, batteryLevel等。

1
2
3
4
5
6

UIDevice.currentDevice.batteryMonitoringEnabled = true

let batteryLevel = UIDevice.currentDevice().batteryLevel

UIDevice.currentDevice.batteryMonitoringEnabled = false

IOKit framework

IOKit framework在IOS中用来跟硬件或内核服务通信,常用于获取硬件详细信息。 首先,需要将IOPowerSources.h,IOPSKeys.h,IOKit三个文件导入到工程中。把batteryMonitoringEnabled置为true,然后即可通过如下代码获取1%精确度的电量信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

-(double) getBatteryLevel{

// returns a blob of power source information in an opaque CFTypeRef

CFTypeRef blob = IOPSCopyPowerSourcesInfo();

// returns a CFArray of power source handles, each of type CFTypeRef

CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

CFDictionaryRef pSource = NULL;

const void *psValue;

// returns the number of values currently in an array

int numOfSources = CFArrayGetCount(sources);

// error in CFArrayGetCount

if (numOfSources == 0) {

NSLog(@"Error in CFArrayGetCount");

return -1.0f;

}

// calculating the remaining energy

for (int i=0; i<numOfSources; i++) {

// returns a CFDictionary with readable information about the specific power source

pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));

if (!pSource) {

NSLog(@"Error in IOPSGetPowerSourceDescription");

return -1.0f;

}

psValue = (CFStringRef) CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

int curCapacity = 0;

int maxCapacity = 0;

double percentage;

psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));

CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));

CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);

percentage = ((double) curCapacity / (double) maxCapacity * 100.0f);

NSLog(@"curCapacity : %d / maxCapacity: %d , percentage: %.1f ", curCapacity, maxCapacity, percentage);

return percentage;

}

return -1.0f;

}

流量

使用Instruments的Network来,这个工具它可以获取到各个线程每秒发送的字节数量,每秒接受字节数量,发包的数量和接收包的数量。跟踪 TCP/IP 和 UDP/IP 连接