获取手机电池状态,手机内存总容量大小,当前可用容量,手机IP地址,手机当前所连接Wi-Fi的名称

闲来没事 研究一写开发中不常用的知识,直接上代码


//  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];
}
@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值