iOS開發 – 如何獲取設備的總容量和可用容量 網絡運營商 3g/wifi判斷 手機型號 – iPhone手機開發技術文章 iPhone軟體開發教學課程

鑌哥最近開發需獲取手機容量,其實這你是一個很好的例子

//硬盤容量

– (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

#import

何判斷當前網絡的運營商

*/

//用來辨別設備所使用網絡的運營商

– (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;

}

發佈留言