您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS runtime 應用實例:取得當前精確網路狀態

編輯:iOS 開發綜合

先前寫過一些 runtime 的筆記,這次透過 runtime 來取得蘋果官方不太希望你直接拿到的東西,例如狀態欄內部控件的屬性。本文會一步步帶你拿到狀態欄中顯示網路狀態的控件,再透過監聽該控件的屬性來取得精確的網路狀態,像是 2G/3G/4G/WIFI。

首先取得狀態欄,再用 runtime 探查它內部的組成結構。

1. 導入 runtime 標頭檔

#import <objc/message.h>

2. 取得應用程式的所有成員變數

#import "ViewController.h"
#import <objc/message.h>

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated
{
    // 狀態欄由當前 app 控制,先取得當前 app
    UIApplication *app = [UIApplication sharedApplication];

    // 遍歷 app 的所有屬性,找到與狀態欄相關的
    unsigned int outCount = 0;
    Ivar *ivars = class_copyIvarList(app.class, &outCount);

    for (int i = 0; i < outCount; i++) {
        Ivar ivar = ivars[i];
        printf("|%s", ivar_getName(ivar));
    }
}

@end

直接執行,可以看到 app 裡確實有個與狀態欄相關的成員變數,接著用 KVC 把它取出來。

3. 透過 KVC 取出狀態欄並遍歷其成員

- (void)viewDidAppear:(BOOL)animated
{
    UIApplication *app = [UIApplication sharedApplication];
    id statusBar = [app valueForKeyPath:@"statusBar"];

    unsigned int outCount = 0;
    Ivar *ivars = class_copyIvarList([statusBar class], &outCount);

    for (int i = 0; i < outCount; i++) {
        Ivar ivar = ivars[i];
        printf("|%s", ivar_getName(ivar));
    }
}

狀態欄裡有個 foregroundView 成員,代表目前顯示的所有視圖,透過 KVC 取出它裡面的所有子視圖。

4. 取得 foregroundView 的子視圖

UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];

for (id child in children) {
    NSLog(@"--%@", [child class]);
}

5. 找出顯示網路狀態的視圖並遍歷其成員變數

UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];

for (id child in children) {
    if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
        unsigned int outCount = 0;
        Ivar *ivars = class_copyIvarList([child class], &outCount);

        for (int i = 0; i < outCount; i++) {
            Ivar ivar = ivars[i];
            printf("|%s", ivar_getName(ivar));
        }
    }
}

6. 透過 KVC 取出 dataNetworkType

if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
    id type = [child valueForKeyPath:@"dataNetworkType"];
    NSLog(@"_dataNetworkType class is %@, value is %@", [type class], type);
}

可見 dataNetworkType 的型別是 NSNumber,值為 5(以上均在模擬器測試)。經測試,可能的值為 1、2、3、5,分別對應 2G、3G、4G 與 WIFI。當沒有網路時,UIStatusBarDataNetworkItemView 會被隱藏,無法取得 dataNetworkType 值。

總結

以下為完整程式碼,並經過真機測試:

- (void)viewDidAppear:(BOOL)animated
{
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];

    int type = 0;
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
    }
    NSLog(@"----%d", type);
}

印出的 type 數字對應的網路狀態依次為:0 - 無網路;1 - 2G;2 - 3G;3 - 4G;5 - WIFI。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved