GLBasic forum

Main forum => GLBasic - en => Topic started by: Millerszone on 2011-Apr-10

Title: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: Millerszone on 2011-Apr-10
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"
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: XanthorXIII on 2011-Apr-11
Thank you Miller. Most handy.
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: AlienMenace on 2011-Nov-29
So where does the top code go? .m?
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: Kitty Hello on 2011-Nov-29
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
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: AlienMenace on 2011-Nov-30
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
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: Kitty Hello on 2011-Nov-30
Silly me. The NSString line is not needed now. Can you retry?
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: AlienMenace on 2011-Nov-30
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.
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: Kitty Hello on 2011-Nov-30
and you did compile for iOS?
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: AlienMenace on 2011-Dec-01
Yep.
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: Kitty Hello on 2011-Dec-01
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.
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: AlienMenace on 2011-Dec-01
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
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: Kitty Hello on 2011-Dec-01
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?  ;/
Title: Re: Detecting the Exact iPhone/iPad/iPod Version you are using
Post by: AlienMenace on 2011-Dec-02
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!