iOS开发 - 如何获取设备的总容量和可用容量 网络运营商 3g/wifi判断 手机型号
镔哥最近开发需获取手机容量,其实这你是一个很好的例子
//硬盘容量
- (float)getTotalDiskSpace
{
float totalSpace;
NSError * error;
NSDictionary * infoDic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[self getHomeDirectory] error: &error];
if (infoDic) {
NSNumber * fileSystemSizeInBytes = [infoDic objectForKey: NSFileSystemSize];
totalSpace = [fileSystemSizeInBytes floatValue]/1024.0f/1024.0f/1024.0f;
return totalSpace;
} else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
return 0;
}
}
- (NSString *)getHomeDirectory
{
NSString * homePath = NSHomeDirectory();
return homePath;
}
/*
如何获取设备的总容量和可用容量
*/
+ (NSNumber *)totalDiskSpace
{
NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
return [fattributes objectForKey:NSFileSystemSize];
}
+ (NSNumber *)freeDiskSpace
{
NSDictionary *fattributes = [[NSFileManager defaultManager] 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 = [NSString stringWithCString:systemInfo.machine encoding: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 ([deviceString isEqualToString:@"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=[Reachability reachabilityWithHostName:@"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中的CTTelephonyNetworkInfo和CTCarrier类型。这些都在iOS 4.0后就有了。
import必要的header:
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
何判断当前网络的运营商
*/
//用来辨别设备所使用网络的运营商
- (NSString*)checkCarrier
{
NSString *ret = [[NSString alloc]init];
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [info subscriberCellularProvider];
if (carrier == nil) {
return @"0";
}
NSString *code = [carrier mobileNetworkCode];
if ([code isEqual: @""]) {
return @"0";
}
if ([code isEqualToString:@"00"] || [code isEqualToString:@"02"] || [code isEqualToString:@"07"]) {
ret = @"移动";
}
if ([code isEqualToString:@"01"]|| [code isEqualToString:@"06"] ) {
ret = @"联通";
}
if ([code isEqualToString:@"03"]|| [code isEqualToString:@"05"] ) {
ret = @"电信";;
}
NSLog(@"%@",ret);
return ret;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。