Detecting the Exact iPhone/iPad/iPod Version you are using

Previous topic - Next topic

Millerszone

Code below will tell you what device you are using. Thought it might come in handy for a few.
I need this for Game Center.
Only tested on iPad and iPhone 3GS/4G

Move the .m file to XCode in the iPhone Folder
Use this code in .m file:
Code (glbasic) Select
//
//  UIDeviceHardware.m
//
//  Used to determine EXACT version of device software is running on.

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

char iDevice[1024]="";

const char* deviceName()
{
    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];
    strcpy(iDevice, [platform UTF8String]);
    free(machine);
    return iDevice;
}


Demo code for GLBasic:
Code (glbasic) Select

LOCAL name$ = ""

?IFDEF IPHONE
IMPORT "C" const char* deviceName()
device$ = deviceName()

SELECT device$
CASE "iPhone1,1" ; device$ = "iPhone 1G"
CASE "iPhone1,2" ; device$ = "iPhone 3G"
CASE "iPhone2,1" ; device$ = "iPhone 3GS"
CASE "iPhone3,1" ; device$ = "iPhone 4"
CASE "iPhone3,2" ; device$ = "Verizon iPhone 4"
CASE "iPod1,1" ; device$ = "iPod Touch 1G"
CASE "iPod2,1" ; device$ = "iPod Touch 2G"
CASE "iPod3,1" ; device$ = "iPod Touch 3G"
CASE "iPod4,1" ; device$ = "iPod Touch 4G"
CASE "iPad1,1" ; device$ = "iPad"
CASE "iPad2,1" ; device$ = "iPad 2 (WiFi)"
CASE "iPad2,2" ; device$ = "iPad 2 (GSM)"
CASE "iPad2,3" ; device$ = "iPad 2 (CDMA)"
CASE "i386" ; device$ = "Simulator"
ENDSELECT
?ENDIF

// your device
PRINT device$, 10, 20,1

SHOWSCREEN
MOUSEWAIT


EDIT: Move to forum "Code Snippets"
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

XanthorXIII

Owlcat has wise

AlienMenace

Apps published: 3

Kitty Hello

Can you try this, inline?

Code (glbasic) Select


[code=glbasic]//
//  UIDeviceHardware.m
//
//  Used to determine EXACT version of device software is running on.

INLINE
typedef unsigned long size_t;
extern "C" int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
const char* deviceName()
{
static char device[1024];
    size_t size = 1024;
    sysctlbyname("hw.machine", device, &size, NULL, 0);

    return device;
}
ENDINLINE

AlienMenace

This is the output when I attempt to build the project:

compiling:
/cygdrive/C/Users/AM/AppData/Local/Temp/glbasic/gpc_temp5.cpp: In function 'const char* __GLBASIC__::deviceName()':
/cygdrive/C/Users/AM/AppData/Local/Temp/glbasic/gpc_temp5.cpp:6194: error: 'NSString' was not declared in this scope
/cygdrive/C/Users/AM/AppData/Local/Temp/glbasic/gpc_temp5.cpp:6194: error: 'platform' was not declared in this scope
/cygdrive/C/Users/AM/AppData/Local/Temp/glbasic/gpc_temp5.cpp:6194: error: expected primary-expression before '[' token
/cygdrive/C/Users/AM/AppData/Local/Temp/glbasic/gpc_temp5.cpp:6194: error: expected `]' before 'stringWithCString'
/cygdrive/C/Users/AM/AppData/Local/Temp/glbasic/gpc_temp5.cpp:6194: error: expected `;' before 'stringWithCString'
*** FATAL ERROR - Please post this output in the forum
Apps published: 3

Kitty Hello

Silly me. The NSString line is not needed now. Can you retry?

AlienMenace

I am trying to use it like this:

Code (glbasic) Select

FUNCTION WHATDEVICE:
LOCAL name$ = ""
LOCAL device$=""

?IFDEF IPHONE
IMPORT "C" const char* deviceName()
device$ = deviceName()

SELECT device$
CASE "iPhone1,1" ; device$ = "iPhone 1G"
CASE "iPhone1,2" ; device$ = "iPhone 3G"
CASE "iPhone2,1" ; device$ = "iPhone 3GS"
CASE "iPhone3,1" ; device$ = "iPhone 4"
CASE "iPhone3,2" ; device$ = "Verizon iPhone 4"
CASE "iPod1,1" ; device$ = "iPod Touch 1G"
CASE "iPod2,1" ; device$ = "iPod Touch 2G"
CASE "iPod3,1" ; device$ = "iPod Touch 3G"
CASE "iPod4,1" ; device$ = "iPod Touch 4G"
CASE "iPad1,1" ; device$ = "iPad"
CASE "iPad2,1" ; device$ = "iPad 2 (WiFi)"
CASE "iPad2,2" ; device$ = "iPad 2 (GSM)"
CASE "iPad2,3" ; device$ = "iPad 2 (CDMA)"
CASE "i386" ; device$ = "Simulator"
ENDSELECT
?ENDIF

RETURN device$
ENDFUNCTION

//  UIDeviceHardware.m
//
//  Used to determine EXACT version of device software is running on.

INLINE
typedef unsigned long size_t;
extern "C" int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
const char* deviceName()
{
static char device[1024];
    size_t size = 1024;
    sysctlbyname("hw.machine", device, &size, NULL, 0);
    return device;
}
ENDINLINE


When you try to run in windows, it gives this (because its an iOS thing?):

Quote
gpc_temp5.o:gpc_temp5.cpp:(.text+0x4301): undefined reference to `_sysctlbyname'
*** FATAL ERROR - Please post this output in the forum
_______________________________________
*** Finished ***
Elapsed: 4.6 sec. Time: 01:12
Build: 0 succeeded.
*** 1 FAILED ***

When I use this in my main program to test on iPad2:

Code (glbasic) Select

PRINT WHATDEVICE(),0,250


And that returns 0. Let me know if I am doing something wrong.

Cheers.
Apps published: 3

Kitty Hello


AlienMenace

Apps published: 3

Kitty Hello

ah. silly me. It's not compiling/linking the true app. Please stick with an external .m file so far that you link against in XCode.

AlienMenace

Okay, well thanks for trying. That does bring me back to my original question of how do I use this? I've not used an .m file before.

Thanks
Apps published: 3

Kitty Hello

just open GLBasic, paste the code, save as .m (not .gbas). Then, compile and put this .m file into your xcode project. (Now, how does that work again?  ;/

AlienMenace

Ok, I added it in to the root of my xCode project as DeviceInfo.m... I though it literally had to be called .m...  :whistle: Got it working, thanks!
Apps published: 3