This Section detects iPhone/iPod Touch/iPad device types Code Share by cocoachina member qqn_pipi, post address http://www.cocoachina.com/bbs/read.php? Tid-20994.html # import Foundation/Foundation. h # import sys/utsname. h Enum {model_iphone_simulator this Section detects the iPhone/iPod Touch/iPad device type code by cocoachina member "qqn_pipi" to share, post address http://www.cocoachina.com/bbs/read.php? Tid-20994.html
# Import <Foundation/Foundation. h>
# Import <sys/utsname. h>
Enum {
Model_iphone_simulator,
Model_ipod_touch,
Model_iphone,
Model_iphone_3g,
Model_ipad
};
@ Interface devicedetection: nsobject
+ (Uint) detectdevice;
+ (Nsstring *) returndevicename :( bool) ignoresimulator;
+ (Bool) isipodtouch;
@ End
# Import "devicedetection. H"
@ Implementation devicedetection
+ (Bool) isipodtouch
{
Int model = [devicedetection detectdevice];
If (model = model_ipod_touch | model = model_ipad ){
// | Model = model_iphone_simulator ){
Return yes;
}
Else {
Return no;
}
}
+ (Uint) detectdevice {
Nsstring * model = [[uidevice currentdevice] model];
// Some iPod Touch return "iPod Touch", others just "iPod"
Nsstring * ipodtouch = @ "iPod Touch ";
Nsstring * ipodtouchlowercase = @ "iPod Touch ";
Nsstring * ipodtouchshort = @ "iPod ";
Nsstring * iPad = @ "iPad ";
Nsstring * iphonesimulator = @ "iPhone Simulator ";
Uint detected;
If ([model compare: iphonesimulator] = nsorderedsame ){
// IPhone Simulator
Detected = model_iphone_simulator;
}
Else if ([model compare: iPad] = nsorderedsame ){
// IPad
Detected = model_ipad;
} Else if ([model compare: ipodtouch] = nsorderedsame ){
// IPod Touch
Detected = model_ipod_touch;
} Else if ([model compare: ipodtouchlowercase] = nsorderedsame ){
// IPod Touch
Detected = model_ipod_touch;
} Else if ([model compare: ipodtouchshort] = nsorderedsame ){
// IPod Touch
Detected = model_ipod_touch;
} Else {
// Cocould be an iPhone V1 or iPhone 3G (Model shocould be "iPhone ")
Struct utsname U;
// U. Machine cocould be "i386" for the simulator, "ipod1, 1" on iPod Touch, "iphone1, 1" on iPhone V1 & "iphone1, 2" on iphone3g
Uname (& U );
If (! Strcmp (U. Machine, "iphone1, 1 ")){
Detected = model_iphone;
} Else {
Detected = model_iphone_3g;
}
}
Return detected;
}
+ (Nsstring *) returndevicename :( bool) ignoresimulator {
Nsstring * returnvalue = @ "unknown ";
Switch ([devicedetection detectdevice]) {
Case model_iphone_simulator:
If (ignoresimulator ){
Returnvalue = @ "iPhone 3G ";
} Else {
Returnvalue = @ "iPhone Simulator ";
}
Break;
Case model_ipod_touch:
Returnvalue = @ "iPod Touch ";
Break;
Case model_iphone:
Returnvalue = @ "iPhone ";
Break;
Case model_iphone_3g:
Returnvalue = @ "iPhone 3G ";
Break;
Default:
Break;
}
Return returnvalue;
}
@ End