This is just a quick post with some source code attached to help other Indie developers in detecting which device their code is running on.

This source code – http://dl.dropbox.com/u/60608455/TestScreen2_0.zip – detects which device the code is running on and loads the correct background image to display.

I hope it helps someone and if you think it can be improved please leave a comment.

Below is the code we are interested in.

if ([UIScreen instancesRespondToSelector:@selector(scale)])

{

//So we grab the screens scale factor – 1.0 = SD screen & 2.0 = HD screen

CGFloat scale = [[UIScreen mainScreen] scale];

 //and now we grab the device model – we are only interested in if it is an iPhone or iPad

NSString* valueDevice = [[UIDevice currentDevice] model];       //and now we grab the device model – we are only interested in if it is an iPhone or iPad

if (scale > 1.0)  //If the scale factor is not 1.0 it is a HD screen

{

CCLOG(@”HD Screen”);

if ([valueDevice rangeOfString:@”iPad”].location == NSNotFound)  //Test to see if our valueDevice NSString does not contains the substring iPad & if it does not it is an iPhone otherwise it is an iPad 

{

NSLog(@”iPhone HD”);  //HD iPhone load HD iPhone graphic

screenToLoad = @”screenHDiPhone.pvr.ccz”;

pListToLoad = @”screenHDiPhone.plist”;

imageToLoad = @”Screen960x720.png”;

} else {

NSLog(@”iPad HD”); //HD iPad load HD iPad graphic

screenToLoad = @”screenHDiPad.pvr.ccz”;

pListToLoad = @”screenHDiPad.plist”;

imageToLoad = @”Screen2048x1536.png”;

}

}else { //and if the scale factor is 1.0 then it is a SD screen

CCLOG(@”SD Screen”);

if ([valueDevice rangeOfString:@”iPad”].location == NSNotFound) //Test to see if our valueDevice NSString does not contains the substring iPad & if it does not it is an iPhone otherwise it is an iPad 

{

NSLog(@”iPhone”);  //SD iPhone load SD iPhone graphic

screenToLoad = @”screeniPhone.pvr.ccz”;

pListToLoad = @”screeniPhone.plist”;

imageToLoad = @”Screen480x360.png”;

} else {

NSLog(@”iPad”); //SD iPad load SD iPad graphic

screenToLoad = @”screeniPad.pvr.ccz”;

pListToLoad = @”screeniPad.plist”;

imageToLoad = @”Screen1024x768.png”;

}

}

}

I hope the above comments helps explain how the code is working.

Craig
Advertisements