الكشف عن نموذج iPhone / iPod Touch الخاص ب iPhone (مكررة
-
12-09-2019 - |
سؤال
ممكن مكررة:
تحديد الجهاز (iPhone، iPod Touch) مع iOS
أقوم بعمل لعبة تستخدم إمكانات Bluetooth من نظير إلى نظير من iPhone (وربما الجيل الثاني ل iPod Touch). ومع ذلك، لإيقاف المستخدمين من محاولة لعب متعددة اللاعبين على iPod 1st Gen و iPhone 2G أحتاج إلى التحقق من طراز الجهاز المحدد.
[[uidevice currentDevice] نموذج] سوف تخبرني فقط إذا كان الجهاز هو "iPhone" أو "iPod touch". هل هناك طريقة للتحقق من نموذج الجهاز المحدد، مثل: "iPhone 3GS"، "iPod touch الجيل الأول" أو شيء من هذا.
تعديل:
هناك فئة إلى UIDEVICE (أعتقد أنه تم إنشاؤها بواسطة إيريكا سادون، وأنا لا آخذ الائتمان لذلك) يستخدم التعليمات البرمجية التالية للحصول على طراز الجهاز المحدد. يمكنك العثور على الفئة بأكملها هنا مع الأشياء المفيدة الأخرى: 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.
المحلول
الأكثر اكتمالا UIDEVICE (الأجهزة) فئة ربما هي http://github.com/erica/uidevice-extension/ (من قبل إيريكا سادون):
[[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"
نصائح أخرى
يمكنك الحصول على رقم طراز الجهاز باستخدام uname
من sys/utsname.h
. وبعد علي سبيل المثال:
#import <sys/utsname.h>
NSString*
machineName()
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
يجب أن تكون النتيجة:
@ "i386" على جهاز محاكاة @ "iPod1،1" على iPod Touch @ "iPod2،1" على iPod Touch الجيل الثاني @ "iPod3،1" على iPod Touch الجيل الثالث @ "iPod4،1" على iPod Touch Rousth Generation @ "iPhone1،1" على iPhone @ "iphone1،2" على iPhone 3G @ "iPhone2،1" على iPhone 3GS @ "iPad1،1" على iPad @ "ipad2،1" على iPad 2 @ "iPad3،1" على باد 3 (AKA جديد iPad) @ "iPhone3،1" على iPhone 4 @ "iphone4،1" على iPhone 4S @ "iphone5،1" على iphone 5 @ "iphone5،2" على iPhone 5
ماذا عن هذا الرمز، إذا تم إصدار إصدار جديد، فسوف تقوم بالمعرف باستخدام آخر جهاز يعرف
#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;
}
BOOL hasHighResScreen = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0) {
hasHighResScreen = YES;
}
}
iPhone 4 iPhone3،1 و iPhone3،2
iPhone 4S هو iPhone4،1
iPad 2 iPad2،1 iPad2،2 و iPad2،3 اعتمادا على الإصدار (GSM إلخ)
iPad 3 iPad3،1 iPad3،2 و iPad3،3 اعتمادا على الإصدار (GSM إلخ)
يرى أسرار iPhone. (قم بالتمرير لأسفل إلى "رموز المنتجات الداخلية")
مصدر جيد آخر هو:anveryhone.com.
NSString* valueDevice = [[UIDevice currentDevice] model];
ثم تحقق مما إذا كانت السلسلة تساوي أي جهاز تبحث عنه مثل:
if(value==@"iPod1,1" )
{}
ويجب أن تكون جيدة للذهاب