How to detect the device type of the running device ?

Forums Programming cocos2d support (graphics engine) How to detect the device type of the running device ?

This topic contains 13 replies, has 7 voices, and was last updated by  lap.felix 3 years, 1 month ago.

Viewing 14 posts - 1 through 14 (of 14 total)
Author Posts
Author Posts
July 22, 2010 at 3:00 am #223401

dummy
Participant
@dummy

Hi,

Which method should I use to get the device type information ?

How could I know whether the device is iPhone 3G, iPhone 4 or iPad ?

When I use [[UIDevice currentDevice] model], it always reports that it is a iPhone simulator, no matter the app is running in iPad or iPhone simulator.

Does this method work properly on real devices ?

Thanks

July 22, 2010 at 4:01 am #291339

petrusali
@petrusali

1. By knowing the current device resolution (noob way but it works)

if (winSize.height == 480)
NSLog(@"It's iPod/iPhone");
else if (winSize.height == 960)
NSLog(@"It's iPhone4");
else if (winSize.height = 1024)
NSLog(@"It's iPad");

2. For iOS 3.2+ by using userInterfaceIdiom, please refer to https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/macro/UI_USER_INTERFACE_IDIOM

3. By checking code support

if ([device respondsToSelector:@selector(...)])

4. By checking system version, the sample is on GSSprite sample code (https://developer.apple.com/iphone/library/samplecode/GLSprite/Listings/Classes_EAGLView_m.html#//apple_ref/doc/uid/DTS40007325-Classes_EAGLView_m-DontLinkElementID_4)

That’s all I know. Hope this will help.

July 22, 2010 at 4:10 am #291340

dummy
Participant
@dummy

Thanks for your answer.

However, in the simulator, the winSize.height always is 480 pixels, even the app runs on iPad simulator.

Should I try it in real devices ?

July 22, 2010 at 4:14 am #291341

petrusali
@petrusali

R u sure about that? I got 1024 pixel height for iPad simulator. That method works on simulator & device.

July 22, 2010 at 4:19 am #291342

dummy
Participant
@dummy

Yes, when I run the app in iPad simulator, winSize.height is 480 pixels.

A “2x” button shows in bottom right corner, and a black border located around my content area.

Do I make something wrong ?

Thanks

July 22, 2010 at 4:26 am #291343

petrusali
@petrusali

Yes u do. You should upgrade your project to for iPad development. How?

1. Expand targets group

2. Ctrl click on your project name

3. Choose Upgrade current target for iPad

July 22, 2010 at 4:34 am #291344

dummy
Participant
@dummy

Cool. Thanks a lot.

Now it shows : cocos2d: surface size: 768×1024

If my target is iPhone 4 (960 x 640), how to do ?

I don’t have a target like iPhone 4 in my xcode config.

July 22, 2010 at 7:48 am #291345

petrusali
@petrusali

For iPhone4, you just need to create a 640×960 background sprite. Then it will acknowledge the size by itself.

July 22, 2010 at 8:05 am #291346

dummy
Participant
@dummy

Thank you.

About coordinate system in iPhone4, should I still use 320×240 ?

Should I need to multiply the x-axis & y-axis values by two manually ?

Cheers

July 22, 2010 at 9:08 am #291347

patgoto
@patgoto

Hi all,

Here the class i wrote for that purpose:

#import <sys/utsname.h>

/**
@brief helper class to detects the device on which the program is running
*/
@interface DeviceDetection : NSObject

enum {
MODEL_UNKNOWN=0,/**< unknown model */
MODEL_IPHONE_SIMULATOR,/**< iphone simulator */
MODEL_IPAD_SIMULATOR,/**< ipad simulator */
MODEL_IPOD_TOUCH_GEN1,/**< ipod touch 1st Gen */
MODEL_IPOD_TOUCH_GEN2,/**< ipod touch 2nd Gen */
MODEL_IPOD_TOUCH_GEN3,/**< ipod touch 3th Gen */
MODEL_IPHONE,/**< iphone */
MODEL_IPHONE_3G,/**< iphone 3G */
MODEL_IPHONE_3GS,/**< iphone 3GS */
MODEL_IPHONE_4, /**< iphone 4 */
MODEL_IPAD/** ipad */
};

/**
get the id of the detected device
*/
+ (uint) detectDevice;
/**
get the string for the detected device
*/
+ (NSString *) returnDeviceName:(BOOL)ignoreSimulator;

@end

#import "DeviceDetection.h"

@implementation DeviceDetection

+ (uint) detectDevice {
NSString *model= [[UIDevice currentDevice] model];
struct utsname u;
uname(&u);

if (!strcmp(u.machine, "iPhone1,1")) {
return MODEL_IPHONE;
} else if (!strcmp(u.machine, "iPhone1,2")){
return MODEL_IPHONE_3G;
} else if (!strcmp(u.machine, "iPhone2,1")){
return MODEL_IPHONE_3GS;
} else if (!strcmp(u.machine, "iPhone3,1")){
return MODEL_IPHONE_4;
} else if (!strcmp(u.machine, "iPod1,1")){
return MODEL_IPOD_TOUCH_GEN1;
} else if (!strcmp(u.machine, "iPod2,1")){
return MODEL_IPOD_TOUCH_GEN2;
} else if (!strcmp(u.machine, "iPod3,1")){
return MODEL_IPOD_TOUCH_GEN3;
} else if (!strcmp(u.machine, "iPad1,1")){
return MODEL_IPAD;
} else if (!strcmp(u.machine, "i386")){
//NSString *iPhoneSimulator = @"iPhone Simulator";
NSString *iPadSimulator = @"iPad Simulator";
if([model compare:iPadSimulator] == NSOrderedSame)
return MODEL_IPAD_SIMULATOR;
else
return MODEL_IPHONE_SIMULATOR;
}
else {
return MODEL_UNKNOWN;
}
}

+ (NSString *) returnDeviceName:(BOOL)ignoreSimulator {
NSString *returnValue = @"Unknown";

switch ([DeviceDetection detectDevice])
{
case MODEL_IPHONE_SIMULATOR:
returnValue = @"iPhone Simulator";
break;
case MODEL_IPOD_TOUCH_GEN1:
returnValue = @"iPod Touch";
break;
case MODEL_IPOD_TOUCH_GEN2:
returnValue = @"iPod Touch";
break;
case MODEL_IPOD_TOUCH_GEN3:
returnValue = @"iPod Touch";
break;
case MODEL_IPHONE:
returnValue = @"iPhone";
break;
case MODEL_IPHONE_3G:
returnValue = @"iPhone 3G";
break;
case MODEL_IPHONE_3GS:
returnValue = @"iPhone 3GS";
break;
case MODEL_IPHONE_4:
returnValue = @"iPhone 4";
break;

case MODEL_IPAD:
returnValue = @"iPad";
break;
default:
break;
}

return returnValue;
}

@end

Hope it helps!

July 29, 2010 at 10:02 pm #291348

Adam Allen
@swelltown

@patgoto Thank you!

March 11, 2011 at 2:12 am #291349

sonofalink
@sonofalink
BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
// do iPad stuff
}
else
{
// iphone/ipod touch stuff
}

March 20, 2011 at 6:22 pm #291350

navratill
@navratill

Hi,

do i have to, when i am porting iPhone app to iPad, change graphics resources(for example background image of app) for iPad by code, or is it possible do that automaticaly…for example something same like for retina display…by -hd suffix. So something like -ipad suffix…

thx!

March 20, 2011 at 9:47 pm #291351

lap.felix
Participant
@lapfelix

Check out the 2nd post of this thread: http://www.cocos2d-iphone.org/forum/topic/13285

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.