首页 > 代码库 > iOS开发 - 如何获取设备的总容量和可用容量 网络运营商 3g/wifi判断 手机型号

iOS开发 - 如何获取设备的总容量和可用容量 网络运营商 3g/wifi判断 手机型号

镔哥最近开发需获取手机容量,其实这你是一个很好的例子

//硬盘容量

- (float)getTotalDiskSpace

{

    float totalSpace;

    NSError * error;

    NSDictionary * infoDic = [[NSFileManagerdefaultManager] attributesOfFileSystemForPath:[selfgetHomeDirectory]error: &error];

    if (infoDic) {

        NSNumber * fileSystemSizeInBytes = [infoDicobjectForKey: NSFileSystemSize];

        totalSpace = [fileSystemSizeInBytes floatValue]/1024.0f/1024.0f/1024.0f;

        return totalSpace;

    } else {

        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [errordomain], (long)[errorcode]);

        return 0;

    } 

}

- (NSString *)getHomeDirectory

{

    NSString * homePath = NSHomeDirectory();

    return homePath;

}


/*

 如何获取设备的总容量和可用容量

 */

+ (NSNumber *)totalDiskSpace

{

    NSDictionary *fattributes = [[NSFileManagerdefaultManager] attributesOfFileSystemForPath:NSHomeDirectory()error:nil];

    return [fattributes objectForKey:NSFileSystemSize];

}

+ (NSNumber *)freeDiskSpace

{

    NSDictionary *fattributes = [[NSFileManagerdefaultManager] attributesOfFileSystemForPath:NSHomeDirectory()error:nil];

    return [fattributes objectForKey:NSFileSystemFreeSize];

}




//手机型号(这跟系统获取的不一样,这是获取iPhone5,4,6之类的)要导入

#import "sys/utsname.h"

+ (NSString*)deviceString

{

    // 需要#import "sys/utsname.h"

    struct utsname systemInfo;

    uname(&systemInfo);

    NSString *deviceString = [NSStringstringWithCString:systemInfo.machineencoding:NSUTF8StringEncoding];

    

    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";

    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";

    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";

    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";

    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";

    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";

    if ([deviceStringisEqualToString:@"iPhone3,2"])   return @"Verizon iPhone 4";

    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";

    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";

    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";

    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";

    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";

    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";

    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";

    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";

    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";

    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";

    NSLog(@"NOTE: Unknown device type: %@", deviceString);

    return deviceString;

}


// 判读是3g还是Wi-Fi 要导入#import"Reachability.h"

       Reachability *reachbility=[ReachabilityreachabilityWithHostName:@"www.baidu.com"];   NetworkStatus status=[reachbility currentReachabilityStatus];

    NSString *netStr;

    //使用 WiFi网络连接

    if (status==ReachableViaWiFi)

    {

          netStr = @"wifi";

        //[[NSUserDefaults standardUserDefaults]setObject:netStr forKey:@"network"];

      

    }

    else if(status ==ReachableViaWWAN)

    {

        netStr = @"3g";

         //[[NSUserDefaults standardUserDefaults]setObject:netStr forKey:@"network"];

      

    }

    NSLog(@"网络是:%@",netStr);

    

/*关于获取运营商信息,需通过CoreTelephony Framework中的CTTelephonyNetworkInfoCTCarrier类型。这些都在iOS 4.0后就有了。

 import必要的header

 #import <CoreTelephony/CTCarrier.h>

 #import <CoreTelephony/CTTelephonyNetworkInfo.h>

 何判断当前网络的运营商

 */

//用来辨别设备所使用网络的运营商

- (NSString*)checkCarrier


{

    

    NSString *ret = [[NSStringalloc]init];

    

    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfoalloc] init];

    

    CTCarrier *carrier = [infosubscriberCellularProvider];

    

    if (carrier == nil) {

        

    

        

        return @"0";

        

    }

    

    NSString *code = [carrier mobileNetworkCode];

    

    if ([code  isEqual:@""]) {

        

    

        

        return @"0";

        

    }

    

    if ([codeisEqualToString:@"00"] || [codeisEqualToString:@"02"] || [codeisEqualToString:@"07"]) {

        

        ret = @"移动";

    }

    

    if ([codeisEqualToString:@"01"]|| [codeisEqualToString:@"06"] ) {

        ret = @"联通";

    }

    

    if ([codeisEqualToString:@"03"]|| [codeisEqualToString:@"05"] ) {

        ret = @"电信";;

    }

    

  NSLog(@"%@",ret);

    return ret;

    

}





iOS开发 - 如何获取设备的总容量和可用容量 网络运营商 3g/wifi判断 手机型号