以下のコードは、アプリを実行しているデバイスのモデル/デバイス名を表示することになっています。たとえば、アプリが第 1 世代の iPod で実行されていた場合、「iPod1,1」と表示する必要があります。アプリケーションを実行すると、ラベルが空になります。私は何を間違っていますか?
#import "ViewController.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize label;
- (void)viewDidLoad
{
label.text = [self platformString];
[self platformCapabilities];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSString *) platform
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding: NSUTF8StringEncoding];
free(machine);
return platform;
}
- (int) platformType
{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return UIDevice1GiPhone;
if ([platform isEqualToString:@"iPhone1,2"]) return UIDevice3GiPhone;
if ([platform isEqualToString:@"iPod1,1"]) return UIDevice1GiPod;
if ([platform isEqualToString:@"iPod2,1"]) return UIDevice2GiPod;
if ([platform hasPrefix:@"iPhone"]) return UIDeviceUnknowniPhone;
if ([platform hasPrefix:@"iPod"]) return UIDeviceUnknowniPod;
return UIDeviceUnknown;
}
- (NSString *) platformString
{
switch ([self platformType])
{
case UIDevice1GiPhone: return IPHONE_1G_NAMESTRING;
case UIDevice3GiPhone: return IPHONE_3G_NAMESTRING;
case UIDeviceUnknowniPhone: return IPHONE_UNKNOWN_NAMESTRING;
case UIDevice1GiPod: return IPOD_1G_NAMESTRING;
case UIDevice2GiPod: return IPOD_2G_NAMESTRING;
case UIDeviceUnknowniPod: return IPOD_UNKNOWN_NAMESTRING;
default: return nil;
}
}
- (int) platformCapabilities
{
switch ([self platformType])
{
case UIDevice1GiPhone: return UIDeviceBuiltInSpeaker | UIDeviceBuiltInCamera | UIDeviceBuiltInMicrophone | UIDeviceSupportsExternalMicrophone | UIDeviceSupportsTelephony | UIDeviceSupportsVibration;
case UIDevice3GiPhone: return UIDeviceSupportsGPS | UIDeviceBuiltInSpeaker | UIDeviceBuiltInCamera | UIDeviceBuiltInMicrophone | UIDeviceSupportsExternalMicrophone | UIDeviceSupportsTelephony | UIDeviceSupportsVibration;
case UIDeviceUnknowniPhone: return UIDeviceBuiltInSpeaker | UIDeviceBuiltInCamera | UIDeviceBuiltInMicrophone | UIDeviceSupportsExternalMicrophone | UIDeviceSupportsTelephony | UIDeviceSupportsVibration;
case UIDevice1GiPod: return 0;
case UIDevice2GiPod: return UIDeviceBuiltInSpeaker | UIDeviceBuiltInMicrophone | UIDeviceSupportsExternalMicrophone;
case UIDeviceUnknowniPod: return 0;
default: return 0;
}
}
@end
また、このコードを拡張して、第 3 世代の iPod、第 4 世代の iPod などを認識するにはどうすればよいでしょうか?