YYKit中对UIDevice的拓展,accumulate knowledge !!!

首先

#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <mach/mach.h>
#include <arpa/inet.h>
#include <ifaddrs.h>

1. Machine Model

参考博客

如何获取机器的型号呢?有人会说使用:[UIDevice currentDevice].model 方案,这是不正确的。因为这个方案只会区分iPhone 和 iPod touch。

在YYKit中,他是通过BSD 底层方法来获取型号,例如:iPhone6,2 、 iPhone4,1 、x86_64 。

YYKit获取Model代码:

#import <sys/sysctl.h>

- (NSString *)machineModel {
    static dispatch_once_t one;
    static NSString *model;
    dispatch_once(&one, ^{
        size_t size;
        sysctlbyname("hw.machine", NULL, &size, NULL, 0);
        char *machine = malloc(size);
        sysctlbyname("hw.machine", machine, &size, NULL, 0);
        model = [NSString stringWithUTF8String:machine];
        free(machine);
    });
    return model;
}

第二种获取Model代码:

#import <sys/utsname.h>

- (NSString *)machineModel
{
    struct utsname systeminfo;
    uname(&systeminfo);
    return [NSString stringWithCString:systeminfo.machine encoding:NSUTF8StringEncoding];
}

2.1 系统库:sysctl.h 和 utsname.h 研究研究

utsname.h

struct  utsname {
    char    sysname[_SYS_NAMELEN];  /* [XSI] Name of OS */
    char    nodename[_SYS_NAMELEN]; /* [XSI] Name of this network node */
    char    release[_SYS_NAMELEN];  /* [XSI] Release level */
    char    version[_SYS_NAMELEN];  /* [XSI] Version level */
    char    machine[_SYS_NAMELEN];  /* [XSI] Hardware type */
};
  • sysname :设备操作系统的名称,例如:Darwin 。
  • nodename :设备本地名称,例如:LVde-iPhone(手机可以通过Settings -> General -> About -> Name 修改)。
  • version :设备操作系统的版本,例如:Darwin Kernel Version 14.0.0: Tue Aug 19 15:08:02 PDT 2014; root:xnu-2783.1.72~8/RELEASE_ARM_S5L8940X
  • machine : 设备的型号,例如:iPhone6, 1 。

sysctl.h : Unix 系统底层获取系统相关的硬件等信息。 参考博客

#define CTL_KERN    1       /* "high kernel": proc, limits 内核核心信息及行为控制 */
#define CTL_VM      2       /* virtual memory  虚拟内存子系统统计数据和行为控制  */
#define CTL_VFS     3       /* file system, mount type is next  虚拟文件系统信息和行为控制 */
#define CTL_NET     4       /* network, see socket.h  网络子系统 */
#define CTL_DEBUG   5       /* debugging parameters  内核调试与信息查询 */
#define CTL_HW      6       /* generic cpu/io  硬件驱动与信息查询 */
#define CTL_MACHDEP 7       /* machine dependent  平台相关的行为控制 */
#define CTL_USER    8       /* user-level  用户环境配置  */

参考代码:

- (int)getSystemInfo:(int)typeSpecifier
{
    size_t size = sizeof(int);
    int results ;
    int mib[2] = {CTL_HW,typeSpecifier};
    sysctl(mib, 2, &results, &size, NULL, 0);
    return results;
}

- (void)test
{
    double totalMem = (double)[self getSystemInfo:HW_PHYSMEM];
    double userMem  = (double)[self getSystemInfo:HW_USERMEM];

    NSLog(@"总内存   - %f GB",totalMem/(1048576000.0));
    NSLog(@"用户内存 - %f GB",userMem/(1048576000.0));
}

//  结果:(// 1G = 1048576000 B)
总内存   - 1.000000 GB
用户内存 - 0.799863 GB

3. Machine Name

通过Machine Model 来匹配设备名:
部分代码:

          @"Watch1,1" : @"Apple Watch",
          @"Watch1,2" : @"Apple Watch",

          @"iPod1,1" : @"iPod touch 1",

          ......

          @"iPhone8,1" : @"iPhone 6s",
          @"iPhone8,2" : @"iPhone 6s Plus",
          @"iPhone8,4" : @"iPhone SE",

github地址


2.IP Address

我的一篇博文 - 2.2节中利用gethostbyname() 解析域名获取IP,但是在测试时候发生崩溃,后来经过资料显示是iOS 6以上不能使用。

YYKit中使用的正式另一种方案 - getifaddrs ;首先#import <ifaddrs.h>

2.1 关于getifaddrs

参考地址:Linux - getifaddrs

The getifaddrs() function creates a linked list of struckures describing the network interfaces of the local system, and stores the address of the first item of the list in * ifap.

3. Network Traffic Bytes

Get the network traffic bytes according to the network traffic type.

3.1 Network Traffic Type


最新文章

  1. C#Winform连接Oracle数据库
  2. 浏览器禁止js打开新窗口
  3. Windows 10下通过蓝牙连接iPhone个人热点进行共享上网
  4. C#--异步显示工作进度
  5. 东大OJ-双塔问题
  6. Java中使用Socket实现服务器端和客户端通讯
  7. PHP中文函数顺序排列一数组且其序数不变
  8. JS Map 简单实现
  9. JVM调优实战
  10. 多个Activity交互的生命周期:
  11. Rweibo , wordcloud
  12. mysql逆向生成 java 实体类
  13. H-Modify Minieye杯第十五届华中科技大学程序设计邀请赛现场赛
  14. 在Linux添加定时任务删除5天前的日志文件
  15. tkinter做一个简单的登陆页面(十六)
  16. java.lang.NumberFormatException: multiple points问题
  17. Java多线程:CAS与java.util.concurrent.atomic
  18. css学习_css背景属性及其应用
  19. 前端框架之Vue.js
  20. SPARQL 入门教程

热门文章

  1. SQL*Plus环境变量设置浅析
  2. ORA-39242 错误
  3. Java借助axis2发布WebService
  4. C#:结构
  5. 初刷LeetCode的感受
  6. linux下使用tar命令
  7. TCP协议
  8. Eclipse 安装 jBPM 插件
  9. 【小白的CFD之旅】16 流程
  10. 【2016-11-15】【坚持学习】【Day26】【WPF 命令绑定到事件】