Skip to content

Instantly share code, notes, and snippets.

@TomLiu
Created August 11, 2013 16:41
Show Gist options
  • Save TomLiu/6205610 to your computer and use it in GitHub Desktop.
Save TomLiu/6205610 to your computer and use it in GitHub Desktop.
Get Mac Disk Info
//
// AppDelegate.m
// demo
//
// Created by 61 on 13-8-11.
// Copyright (c) 2013年 61. All rights reserved.
//
#import "AppDelegate.h"
#import <IOKit/IOKitLib.h>
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"%@", [self getDiskSerialNumber]);
}
- (NSString *)getDiskSerialNumber
{
kern_return_t kr;
io_iterator_t io_objects;
io_service_t io_service;
NSString *serialNumber = nil;
CFMutableDictionaryRef service_properties = CFDictionaryCreateMutable(NULL, 0, NULL,NULL);
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceNameMatching("AppleAHCIDiskDriver"), &io_objects);
if(kr != KERN_SUCCESS)
return nil;
while((io_service = IOIteratorNext(io_objects))) {
kr = IORegistryEntryCreateCFProperties(io_service, &service_properties, kCFAllocatorDefault, kNilOptions);
if(kr == KERN_SUCCESS) {
NSDictionary * serviceInfo = (__bridge NSDictionary *)service_properties;
NSLog(@"%@", serviceInfo);
serialNumber = [serviceInfo objectForKey:@"Serial Number"];
CFRelease(service_properties);
}
IOObjectRelease(io_service);
}
IOObjectRelease(io_objects);
return serialNumber;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment