闲来没事 研究一写开发中不常用的知识,直接上代码
// ViewController.m
// GetMemory
//
// Created by 冯闯 on 17/4/25.
// Copyright © 2017 年 冯闯 . All rights reserved.
//
#import "ViewController.h"
#include <sys/mount.h> // 获取手机磁盘容量
#import <SystemConfiguration/CaptiveNetwork.h> // 获取手机当前 Wi-Fi 名称
#import <ifaddrs.h> // 获取手机 IP
#import <arpa/inet.h> // 获取手机 IP
#define USER_APP_PATH @“/ User / Applications /”
@interface ViewController ()
@end
@implementation ViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
[ self getTotalDiskSize ];
[ self getAvailableDiskSize ];
[ self getWifiName ];
[ self getvsion ];
[ self deviceIPAdress ];
[ self getBatteryQuantity ];
}
//******************************************************
// 获取手机磁盘总容量
-( long long )getTotalDiskSize
{
struct statfs buf;
unsigned long long freeSpace = - 1 ;
if ( statfs ( "/var" , &buf) >= 0 )
{
freeSpace = ( unsigned long long )(buf. f_bsize * buf. f_blocks );
}
NSLog( @" 手机总容量 %@" ,[ self fileSizeToString :freeSpace]);
return freeSpace;
}
// 获取手机磁盘当前可用容量
-( long long )getAvailableDiskSize
{
struct statfs buf;
unsigned long long freeSpace = - 1 ;
if ( statfs ( "/var" , &buf) >= 0 )
{
freeSpace = ( unsigned long long )(buf.f_bsize * buf.f_bavail);
}
NSLog( @" 手机可用容量 %@" ,[ self fileSizeToString :freeSpace]);
return freeSpace;
}
-( NSString *)fileSizeToString:( unsigned long long )fileSize
{
NSInteger KB = 1024 ;
NSInteger MB = KB*KB;
NSInteger GB = MB*KB;
if (fileSize < 10 ) {
return @"0 B" ;
} else if (fileSize < KB) {
return @"< 1 KB" ;
} else if (fileSize < MB) {
return [ NSString stringWithFormat : @"%.1f KB" ,(( CGFloat )fileSize)/KB];
} else if (fileSize < GB) {
return [ NSString stringWithFormat : @"%.1f MB" ,(( CGFloat )fileSize)/MB];
} else {
return [ NSString stringWithFormat : @"%.1f GB" ,(( CGFloat )fileSize)/GB];
}
}
//******************************************************
// 获取手机当前所连接 Wi-Fi 的名称
- ( NSString *)getWifiName
{
NSString *wifiName = nil ;
CFArrayRef wifiInterfaces = CNCopySupportedInterfaces ();
if (!wifiInterfaces) {
return nil ;
}
NSArray *interfaces = ( __bridge NSArray *)wifiInterfaces;
for ( NSString *interfaceName in interfaces) {
CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo (( __bridge CFStringRef )(interfaceName));
if (dictRef) {
NSDictionary *networkInfo = ( __bridge NSDictionary *)dictRef;
wifiName = [networkInfo objectForKey :( __bridge NSString *) kCNNetworkInfoKeySSID ];
CFRelease (dictRef);
}
}
CFRelease (wifiInterfaces);
NSLog( @" 手机连接 Wi-Fi 名称是 :%@" ,wifiName);
return wifiName;
}
//******************************************************
// 获取手机当前系统版本
-( CGFloat )getvsion{
CGFloat vsion = [[[ UIDevice currentDevice ] systemVersion ] floatValue ] ;
NSLog( @" 手机系用版本: %f" ,vsion);
return vsion;
}
//******************************************************
// 获取手机 IP
- ( NSString *)deviceIPAdress {
NSString *address = @"an error occurred when obtaining ip address" ;
struct ifaddrs *interfaces = NULL ;
struct ifaddrs *temp_addr = NULL ;
int success = 0 ;
success = getifaddrs (&interfaces);
if (success == 0 ) { // 0 表示获取成功
temp_addr = interfaces;
while (temp_addr != NULL ) {
if ( temp_addr-> ifa_addr -> sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if ([[ NSString stringWithUTF8String :temp_addr-> ifa_name ] isEqualToString : @"en0" ]) {
// Get NSString from C String
address = [ NSString stringWithUTF8String : inet_ntoa ((( struct sockaddr_in *)temp_addr-> ifa_addr )-> sin_addr )];
}
}
temp_addr = temp_addr-> ifa_next ;
}
}
freeifaddrs (interfaces);
NSLog( @" 手机 IP 地址: %@" ,address);
return address;
}
//******************************************************
// 获取电池电量
-( CGFloat )getBatteryQuantity
{
NSLog( @" 手机当前电量: %f" ,[[UIDevice currentDevice ] batteryLevel]);
return [[UIDevice currentDevice] batteryLevel];
}
//
// Created by 冯闯 on 17/4/25.
// Copyright © 2017 年 冯闯 . All rights reserved.
//
#import "ViewController.h"
#include <sys/mount.h> // 获取手机磁盘容量
#import <SystemConfiguration/CaptiveNetwork.h> // 获取手机当前 Wi-Fi 名称
#import <ifaddrs.h> // 获取手机 IP
#import <arpa/inet.h> // 获取手机 IP
#define USER_APP_PATH @“/ User / Applications /”
@interface ViewController ()
@end
@implementation ViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
[ self getTotalDiskSize ];
[ self getAvailableDiskSize ];
[ self getWifiName ];
[ self getvsion ];
[ self deviceIPAdress ];
[ self getBatteryQuantity ];
}
//******************************************************
// 获取手机磁盘总容量
-( long long )getTotalDiskSize
{
struct statfs buf;
unsigned long long freeSpace = - 1 ;
if ( statfs ( "/var" , &buf) >= 0 )
{
freeSpace = ( unsigned long long )(buf. f_bsize * buf. f_blocks );
}
NSLog( @" 手机总容量 %@" ,[ self fileSizeToString :freeSpace]);
return freeSpace;
}
// 获取手机磁盘当前可用容量
-( long long )getAvailableDiskSize
{
struct statfs buf;
unsigned long long freeSpace = - 1 ;
if ( statfs ( "/var" , &buf) >= 0 )
{
freeSpace = ( unsigned long long )(buf.f_bsize * buf.f_bavail);
}
NSLog( @" 手机可用容量 %@" ,[ self fileSizeToString :freeSpace]);
return freeSpace;
}
-( NSString *)fileSizeToString:( unsigned long long )fileSize
{
NSInteger KB = 1024 ;
NSInteger MB = KB*KB;
NSInteger GB = MB*KB;
if (fileSize < 10 ) {
return @"0 B" ;
} else if (fileSize < KB) {
return @"< 1 KB" ;
} else if (fileSize < MB) {
return [ NSString stringWithFormat : @"%.1f KB" ,(( CGFloat )fileSize)/KB];
} else if (fileSize < GB) {
return [ NSString stringWithFormat : @"%.1f MB" ,(( CGFloat )fileSize)/MB];
} else {
return [ NSString stringWithFormat : @"%.1f GB" ,(( CGFloat )fileSize)/GB];
}
}
//******************************************************
// 获取手机当前所连接 Wi-Fi 的名称
- ( NSString *)getWifiName
{
NSString *wifiName = nil ;
CFArrayRef wifiInterfaces = CNCopySupportedInterfaces ();
if (!wifiInterfaces) {
return nil ;
}
NSArray *interfaces = ( __bridge NSArray *)wifiInterfaces;
for ( NSString *interfaceName in interfaces) {
CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo (( __bridge CFStringRef )(interfaceName));
if (dictRef) {
NSDictionary *networkInfo = ( __bridge NSDictionary *)dictRef;
wifiName = [networkInfo objectForKey :( __bridge NSString *) kCNNetworkInfoKeySSID ];
CFRelease (dictRef);
}
}
CFRelease (wifiInterfaces);
NSLog( @" 手机连接 Wi-Fi 名称是 :%@" ,wifiName);
return wifiName;
}
//******************************************************
// 获取手机当前系统版本
-( CGFloat )getvsion{
CGFloat vsion = [[[ UIDevice currentDevice ] systemVersion ] floatValue ] ;
NSLog( @" 手机系用版本: %f" ,vsion);
return vsion;
}
//******************************************************
// 获取手机 IP
- ( NSString *)deviceIPAdress {
NSString *address = @"an error occurred when obtaining ip address" ;
struct ifaddrs *interfaces = NULL ;
struct ifaddrs *temp_addr = NULL ;
int success = 0 ;
success = getifaddrs (&interfaces);
if (success == 0 ) { // 0 表示获取成功
temp_addr = interfaces;
while (temp_addr != NULL ) {
if ( temp_addr-> ifa_addr -> sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if ([[ NSString stringWithUTF8String :temp_addr-> ifa_name ] isEqualToString : @"en0" ]) {
// Get NSString from C String
address = [ NSString stringWithUTF8String : inet_ntoa ((( struct sockaddr_in *)temp_addr-> ifa_addr )-> sin_addr )];
}
}
temp_addr = temp_addr-> ifa_next ;
}
}
freeifaddrs (interfaces);
NSLog( @" 手机 IP 地址: %@" ,address);
return address;
}
//******************************************************
// 获取电池电量
-( CGFloat )getBatteryQuantity
{
NSLog( @" 手机当前电量: %f" ,[[UIDevice currentDevice ] batteryLevel]);
return [[UIDevice currentDevice] batteryLevel];
}
@end