runtime 获取网络状态

之前写过runtime的一些东西,这次通过runtime获取一些苹果官方不想让你拿到的东西,比如,状态栏内部的控件属性。本文将通过runtime带你一步步拿到状态栏中显示网络状态的控件,然后通过监测该控件的属性来获取当前精确网络状态,比如2G/3G/4G/WIFI。

首先,我们需要拿到状态栏,然后通过runtime去探讨状态栏内部的组成结构。

1、导入运行时头文件

<span style="color: rgb(0, 0, 255);">#import</span> <span class="tag"><<span class="title">objc</span>/<span class="attribute">message.h</span>></span>

2、编写运行时代码,获取到当前应用程序的所有成员变量

<span style="color: rgb(0, 128, 128);"> <span class="number">1</span></span> <span style="color: rgb(0, 0, 255);">#<span class="keyword">import</span></span> <span style="color: rgb(128, 0, 0);">"</span><span class="string"><span style="color: rgb(128, 0, 0);">ViewController.h</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>
<span style="color: rgb(0, 128, 128);"> <span class="number">2</span></span> <span style="color: rgb(0, 0, 255);">#<span class="keyword">import</span></span> <objc/message.h>
<span style="color: rgb(0, 128, 128);"> <span class="number">3</span></span> 
<span style="color: rgb(0, 128, 128);"> <span class="number">4</span></span> <span style="color: rgb(0, 0, 255);">@interface</span> ViewController ()
<span style="color: rgb(0, 128, 128);"> <span class="number">5</span></span> 
<span style="color: rgb(0, 128, 128);"> <span class="number">6</span></span> <span style="color: rgb(0, 0, 255);">@end</span>
<span style="color: rgb(0, 128, 128);"> <span class="number">7</span></span> 
<span style="color: rgb(0, 128, 128);"> <span class="number">8</span></span> <span style="color: rgb(0, 0, 255);">@implementation</span> ViewController
<span style="color: rgb(0, 128, 128);"> <span class="number">9</span></span> 
<span style="color: rgb(0, 128, 128);">10</span> - (<span style="color: rgb(0, 0, 255);">void</span>)viewDidAppear:(BOOL)animated
<span style="color: rgb(0, 128, 128);">11</span> {
<span style="color: rgb(0, 128, 128);">12</span>     <span style="color: rgb(0, 128, 0);">//</span><span class="comment"><span style="color: rgb(0, 128, 0);"> 状态栏是由当前app控制的,首先获取当前app</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);">13</span>     UIApplication *app = [UIApplication sharedApplication];
<span style="color: rgb(0, 128, 128);">14</span>     
<span style="color: rgb(0, 128, 128);">15</span>     <span style="color: rgb(0, 128, 0);">//</span><span class="comment"><span style="color: rgb(0, 128, 0);"> 遍历当前app的所有属性,找到关于状态栏的</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);">16</span>     unsigned <span style="color: rgb(0, 0, 255);">int</span> outCount = <span style="color: rgb(128, 0, 128);">0</span>;
<span style="color: rgb(0, 128, 128);">17</span>     
<span style="color: rgb(0, 128, 128);">18</span>     Ivar *ivars = class_copyIvarList(app.<span style="color: rgb(0, 0, 255);">class</span>, &outCount);
<span style="color: rgb(0, 128, 128);">19</span>     
<span style="color: rgb(0, 128, 128);">20</span>     <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">int</span> i = <span style="color: rgb(128, 0, 128);">0</span>; i < outCount; i++) {
<span style="color: rgb(0, 128, 128);">21</span>         Ivar ivar = ivars[i];
<span style="color: rgb(0, 128, 128);">22</span>         printf(<span style="color: rgb(128, 0, 0);">"</span><span class="string"><span style="color: rgb(128, 0, 0);">|%s</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>, ivar_getName(ivar));
<span style="color: rgb(0, 128, 128);">23</span>     }
<span style="color: rgb(0, 128, 128);">24</span> }
<span style="color: rgb(0, 128, 128);">25</span> 
<span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 255);">@end</span>

直接运行,可以看到打印结果为:

3、可以看app里确实有个关于状态栏的成员变量,我们通过KVC取出它,之前很少用KVC,几乎不知道为什么用,现在知道了,其实在runtime里可以很方便的得到成员

<span style="color: rgb(0, 128, 128);"> <span class="number">1</span></span> - (<span style="color: rgb(0, 0, 255);">void</span>)viewDidAppear:(BOOL)animated
<span style="color: rgb(0, 128, 128);"> <span class="number">2</span></span> {
<span style="color: rgb(0, 128, 128);"> <span class="number">3</span></span>     <span style="color: rgb(0, 128, 0);">//</span><span class="comment"><span style="color: rgb(0, 128, 0);"> 状态栏是由当前app控制的,首先获取当前app</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);"> <span class="number">4</span></span>     UIApplication *app = [UIApplication sharedApplication];
<span style="color: rgb(0, 128, 128);"> <span class="number">5</span></span>     
<span style="color: rgb(0, 128, 128);"> <span class="number">6</span></span>     <span style="color: rgb(0, 0, 255);">id</span> statusBar = [app valueForKeyPath:<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">statusBar</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>];
<span style="color: rgb(0, 128, 128);"> <span class="number">7</span></span>     
<span style="color: rgb(0, 128, 128);"> <span class="number">8</span></span>     <span style="color: rgb(0, 128, 0);">//</span><span class="comment"><span style="color: rgb(0, 128, 0);"> 遍历状态栏的所有成员</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);"> <span class="number">9</span></span>     unsigned <span style="color: rgb(0, 0, 255);">int</span> outCount = <span style="color: rgb(128, 0, 128);">0</span>;
<span style="color: rgb(0, 128, 128);">10</span>     Ivar *ivars = class_copyIvarList([statusBar <span style="color: rgb(0, 0, 255);">class</span>], &outCount);
<span style="color: rgb(0, 128, 128);">11</span>     
<span style="color: rgb(0, 128, 128);">12</span>     <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">int</span> i = <span style="color: rgb(128, 0, 128);">0</span>; i < outCount; i++) {
<span style="color: rgb(0, 128, 128);">13</span>         Ivar ivar = ivars[i];
<span style="color: rgb(0, 128, 128);">14</span>         printf(<span style="color: rgb(128, 0, 0);">"</span><span class="string"><span style="color: rgb(128, 0, 0);">|%s</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>, ivar_getName(ivar));
<span style="color: rgb(0, 128, 128);">15</span>     }
<span style="color: rgb(0, 128, 128);">16</span> }

运行后可以看到打印结果为

4、状态栏里有foregroundView这个成员,应该代表着所有当前显示的视图,通过KVC取出它里面的所有子视图

<span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 128, 0);">//</span><span class="comment"><span style="color: rgb(0, 128, 0);"> 状态栏是由当前app控制的,首先获取当前app</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);">2</span>     UIApplication *app = [UIApplication sharedApplication];
<span style="color: rgb(0, 128, 128);">3</span>     
<span style="color: rgb(0, 128, 128);">4</span>     NSArray *children = [[[app valueForKeyPath:<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">statusBar</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>] valueForKeyPath:<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">foregroundView</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>] subviews];
<span style="color: rgb(0, 128, 128);">5</span>     
<span style="color: rgb(0, 128, 128);">6</span>     <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">id</span> child <span style="color: rgb(0, 0, 255);">in</span> children) {
<span style="color: rgb(0, 128, 128);">7</span>         NSLog(<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">--%@</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>, [child <span style="color: rgb(0, 0, 255);">class</span>]);
<span style="color: rgb(0, 128, 128);">8</span>     }

打印结果为

5、遍历数组,取出用于显示网络状态的视图,并遍历其内部的所有成员变量

<span style="color: rgb(0, 128, 128);"> <span class="number">1</span></span> <span style="color: rgb(0, 128, 0);">//</span><span class="comment"><span style="color: rgb(0, 128, 0);"> 状态栏是由当前app控制的,首先获取当前app</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);"> <span class="number">2</span></span>     UIApplication *app = [UIApplication sharedApplication];
<span style="color: rgb(0, 128, 128);"> <span class="number">3</span></span>     
<span style="color: rgb(0, 128, 128);"> <span class="number">4</span></span>     NSArray *children = [[[app valueForKeyPath:<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">statusBar</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>] valueForKeyPath:<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">foregroundView</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>] subviews];
<span style="color: rgb(0, 128, 128);"> <span class="number">5</span></span>     
<span style="color: rgb(0, 128, 128);"> <span class="number">6</span></span>     <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">id</span> child <span style="color: rgb(0, 0, 255);">in</span> children) {
<span style="color: rgb(0, 128, 128);"> <span class="number">7</span></span>         <span style="color: rgb(0, 0, 255);">if</span> ([child isKindOfClass:NSClassFromString(<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">UIStatusBarDataNetworkItemView</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>)]) {
<span style="color: rgb(0, 128, 128);"> <span class="number">8</span></span>             <span style="color: rgb(0, 128, 0);">//</span><span class="comment"><span style="color: rgb(0, 128, 0);"> 遍历当前状态栏的所有属性,找到关于状态栏的</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);"> <span class="number">9</span></span>             unsigned <span style="color: rgb(0, 0, 255);">int</span> outCount = <span style="color: rgb(128, 0, 128);">0</span>;
<span style="color: rgb(0, 128, 128);">10</span>             Ivar *ivars = class_copyIvarList([child <span style="color: rgb(0, 0, 255);">class</span>], &outCount);
<span style="color: rgb(0, 128, 128);">11</span>             
<span style="color: rgb(0, 128, 128);">12</span>             <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">int</span> i = <span style="color: rgb(128, 0, 128);">0</span>; i < outCount; i++) {
<span style="color: rgb(0, 128, 128);">13</span>                 Ivar ivar = ivars[i];
<span style="color: rgb(0, 128, 128);">14</span>                 printf(<span style="color: rgb(128, 0, 0);">"</span><span class="string"><span style="color: rgb(128, 0, 0);">|%s</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>, ivar_getName(ivar));
<span style="color: rgb(0, 128, 128);">15</span>             }
<span style="color: rgb(0, 128, 128);">16</span>         }
<span style="color: rgb(0, 128, 128);">17</span>     }

打印结果为

6、下面通过KVC,取出dataNetworkType

<span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 255);">if</span> ([child isKindOfClass:NSClassFromString(<span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);">UIStatusBarDataNetworkItemView</span><span style="color: rgb(128, 0, 0);">"</span><span class="string">)]) {
<span style="color: rgb(0, 128, 128);">2</span>             <span style="color: rgb(0, 0, 255);">id</span> type = [child valueForKeyPath:<span style="color: rgb(128, 0, 0);"><span class="variable">@"</span></span><span style="color: rgb(128, 0, 0);">dataNetworkType</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>];
<span style="color: rgb(0, 128, 128);">3</span>             NSLog(<span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);">_dataNetworkType class is <span class="variable">%@</span>, value is <span class="variable">%@</span></span><span style="color: rgb(128, 0, 0);">"</span><span class="string">, [type <span style="color: rgb(0, 0, 255);">class</span>], type);
<span style="color: rgb(0, 128, 128);">4</span>         }</span>

打印结果为: 

可见,dataNetworkType类型是NSNumber,值是5。【以上均为模拟器测试】

经过测试,发现,可能的值为 1,2,3,5 分别对应的网络状态是2G、3G、4G及WIFI。 当没有网络时,隐藏UIStatusBarDataNetworkItemView,无法获取dataNetworkType值

总结:

以下是完整的代码,并经过真机测试:

<span style="color: rgb(0, 128, 128);"> <span class="number">1</span></span> - (<span style="color: rgb(0, 0, 255);">void</span>)viewDidAppear:(BOOL)animated
<span style="color: rgb(0, 128, 128);"> <span class="number">2</span></span> {
<span style="color: rgb(0, 128, 128);"> <span class="number">3</span></span>     <span style="color: rgb(0, 128, 0);">//</span><span class="comment"><span style="color: rgb(0, 128, 0);"> 状态栏是由当前app控制的,首先获取当前app</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);"> <span class="number">4</span></span>     UIApplication *app = [UIApplication sharedApplication];
<span style="color: rgb(0, 128, 128);"> <span class="number">5</span></span>     
<span style="color: rgb(0, 128, 128);"> <span class="number">6</span></span>     NSArray *children = [[[app valueForKeyPath:<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">statusBar</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>] valueForKeyPath:<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">foregroundView</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>] subviews];
<span style="color: rgb(0, 128, 128);"> <span class="number">7</span></span>     
<span style="color: rgb(0, 128, 128);"> <span class="number">8</span></span>     <span style="color: rgb(0, 0, 255);">int</span> type = <span style="color: rgb(128, 0, 128);">0</span>;
<span style="color: rgb(0, 128, 128);"> <span class="number">9</span></span>     <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">id</span> child <span style="color: rgb(0, 0, 255);">in</span> children) {
<span style="color: rgb(0, 128, 128);">10</span>         <span style="color: rgb(0, 0, 255);">if</span> ([child isKindOfClass:NSClassFromString(<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">UIStatusBarDataNetworkItemView</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>)]) {
<span style="color: rgb(0, 128, 128);">11</span>             type = [[child valueForKeyPath:<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">dataNetworkType</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>] intValue];
<span style="color: rgb(0, 128, 128);">12</span>         }
<span style="color: rgb(0, 128, 128);">13</span>     }
<span style="color: rgb(0, 128, 128);">14</span>     NSLog(<span style="color: rgb(128, 0, 0);">@"</span><span class="string"><span style="color: rgb(128, 0, 0);">----%d</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span>, type); 
<span style="color: rgb(0, 128, 128);">15</span> }

打印出的type数字对应的网络状态依次是 : 0 - 无网络 ; 1 - 2G ; 2 - 3G ; 3 - 4G ; 5 - WIFI

建议: 将获取的UIStatusBarDataNetworkItemView保存起来,定时去取它的dataNetworkType,这样就可以实时监控网络状态啦(KVO在这里是行不通的哟)

当然,此方法存在一定的局限性,比如当状态栏被隐藏的时候,无法使用此方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值