107

重複の可能性:
iOS でデバイス (iPhone、iPod Touch) を特定する

私は、iPhone (およびおそらく第 2 世代の iPod touch) のピアツーピア Bluetooth 機能を利用するゲームを作成しています。ただし、ユーザーが第 1 世代の iPod と iPhone 2G でマルチプレイヤーをプレイしようとするのを防ぐには、特定のデバイス モデルを確認する必要があります。

[[UIDevice currentDevice] model] は、デバイスが「iPhone」か「iPod touch」かだけを教えてくれます。「iPhone 3GS」、「iPod touch 第 1 世代」など、特定のデバイス モデルを確認する方法はありますか。

編集:

特定のデバイス モデルを取得するために次のコードを使用する UIDevice のカテゴリがあります (Erica Sadun によって作成されたと思いますが、私は信用しません)。他の便利なものとともに、ここでカテゴリ全体を見つけることができます: https://github.com/erica/uidevice-extension

#include <sys/types.h>
#include <sys/sysctl.h>

@implementation UIDevice (Hardware)

/*
 Platforms
 iPhone1,1 -> iPhone 1G
 iPhone1,2 -> iPhone 3G 
 iPod1,1   -> iPod touch 1G 
 iPod2,1   -> iPod touch 2G 
*/

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

これは機能し、これを使用するアプリは最近 AppStore で承認されました。

4

6 に答える 6

152

unamefromを使用して、デバイスのモデル番号を取得できますsys/utsname.h。例えば:

#import <sys/utsname.h>

NSString*
machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
}

結果は次のようになります。

シミュレータ上の @"i386"
iPod Touchの@"iPod1,1"
@"iPod2,1" (iPod Touch 第 2 世代)
@"iPod3,1" (iPod Touch 第 3 世代)
iPod Touch 第 4 世代の @"iPod4,1"
@「iPhone1,1」iPhone上
@「iPhone1,2」iPhone 3G
@「iPhone2,1」iPhone 3GS
@"iPad1,1" iPad で
@"iPad2,1" (iPad 2)
@"iPad3,1" iPad 3 (別名、新しい iPad)
@iPhone4の「iPhone3,1」
@"iPhone4,1" iPhone 4S
@「iPhone5,1」iPhone 5
@「iPhone5,2」iPhone 5
于 2009-07-10T11:00:56.487 に答える
66

最も完全な UIDevice (ハードウェア) カテゴリは、おそらくhttp://github.com/erica/uidevice-extension/ (Erica Sadun による) です。

[[UIDevice currentDevice] platformType]   // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"
于 2010-07-29T17:32:34.647 に答える
23

このコードはどうですか。新しいバージョンがリリースされた場合、最後に知っているデバイスで識別します

#include <sys/types.h>
#include <sys/sysctl.h>

- (NSString *)getModel {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model = malloc(size);
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *sDeviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    free(model);                              
    if ([sDeviceModel isEqual:@"i386"])      return @"Simulator";  //iPhone Simulator
    if ([sDeviceModel isEqual:@"iPhone1,1"]) return @"iPhone1G";   //iPhone 1G
    if ([sDeviceModel isEqual:@"iPhone1,2"]) return @"iPhone3G";   //iPhone 3G
    if ([sDeviceModel isEqual:@"iPhone2,1"]) return @"iPhone3GS";  //iPhone 3GS
    if ([sDeviceModel isEqual:@"iPhone3,1"]) return @"iPhone4 AT&T";  //iPhone 4 - AT&T
    if ([sDeviceModel isEqual:@"iPhone3,2"]) return @"iPhone4 Other";  //iPhone 4 - Other carrier
    if ([sDeviceModel isEqual:@"iPhone3,3"]) return @"iPhone4";    //iPhone 4 - Other carrier
    if ([sDeviceModel isEqual:@"iPhone4,1"]) return @"iPhone4S";   //iPhone 4S
    if ([sDeviceModel isEqual:@"iPhone5,1"]) return @"iPhone5";    //iPhone 5 (GSM)
    if ([sDeviceModel isEqual:@"iPod1,1"])   return @"iPod1stGen"; //iPod Touch 1G
    if ([sDeviceModel isEqual:@"iPod2,1"])   return @"iPod2ndGen"; //iPod Touch 2G
    if ([sDeviceModel isEqual:@"iPod3,1"])   return @"iPod3rdGen"; //iPod Touch 3G
    if ([sDeviceModel isEqual:@"iPod4,1"])   return @"iPod4thGen"; //iPod Touch 4G
    if ([sDeviceModel isEqual:@"iPad1,1"])   return @"iPadWiFi";   //iPad Wifi
    if ([sDeviceModel isEqual:@"iPad1,2"])   return @"iPad3G";     //iPad 3G
    if ([sDeviceModel isEqual:@"iPad2,1"])   return @"iPad2";      //iPad 2 (WiFi)
    if ([sDeviceModel isEqual:@"iPad2,2"])   return @"iPad2";      //iPad 2 (GSM)
    if ([sDeviceModel isEqual:@"iPad2,3"])   return @"iPad2";      //iPad 2 (CDMA)

    NSString *aux = [[sDeviceModel componentsSeparatedByString:@","] objectAtIndex:0];

//If a newer version exist
    if ([aux rangeOfString:@"iPhone"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] intValue];
        if (version == 3) return @"iPhone4"
        if (version >= 4) return @"iPhone4s";

    }
    if ([aux rangeOfString:@"iPod"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPod" withString:@""] intValue];
        if (version >=4) return @"iPod4thGen";
    }
    if ([aux rangeOfString:@"iPad"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPad" withString:@""] intValue];
        if (version ==1) return @"iPad3G";
        if (version >=2) return @"iPad2";
    }
    //If none was found, send the original string
    return sDeviceModel;
}
于 2011-12-12T17:59:07.800 に答える
4

iPhone 4はiPhone3,1およびiPhone3,2です。iPhone4S

iPhone4,1です。iPad2はバージョン(GSMなど)に応じて
iPad2,1 iPad2,2およびiPad2,3です。iPad3はiPad3,1、iPad3,2およびiPad3,3に依存します。バージョン(GSMなど)

Iphoneの秘密を参照してください(「内部製品コード」までスクロールダウンしてください)

もう1つの良い情報源は: everyiphone.com

于 2011-06-20T09:14:19.870 に答える
4
BOOL hasHighResScreen = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
    CGFloat scale = [[UIScreen mainScreen] scale];
    if (scale > 1.0) {
        hasHighResScreen = YES;
    }
}
于 2010-12-04T13:01:09.773 に答える