简体   繁体   中英

Cocoa touch - obtaining device information

I go this task to read some device information, like device name, type, space in disk and iOS version. I got some ways to know if the device is an iPad, iPhone or retina, but im clueless on knowing any further about the device.

Reading iOS Version:

NSString* iOSVersion = [[UIDevice currentDevice] systemVersion];

Reading iPad Model:

BOOL isIPad2 = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
                            [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]);
NSString*    iPadModel = [[UIDevice currentDevice] model];
            if (isIPad2)
                iPadModel = @"iPad2";

Reading free/total space disk:

- (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];
}

To find ios Version

[[UIDevice currentDevice] systemVersion];

To find space in disk

float totalSpace = 0.0f;
float totalFreeSpace = 0.0f;
NSError *error = nil;  
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];  

if (dictionary) {  
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];  
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes floatValue];
totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
NSLog(@"Memory Capacity of %f MiB with %f MiB Free memory available.", ((totalSpace/1024.0f)/1024.0f), ((totalFreeSpace/1024.0f)/1024.0f));
} else {  
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);  
}  

NSLog(@"%f",totalFreeSpace);

To find the Device name

NSLog(@"%@",[[UIDevice currentDevice] name]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM