views:

83

answers:

3

So I've got a function that really helps when I'm crafting device specific URLS but I'd like to place it in a global header file so any class could use it easily

- (NSString *)deviceType
{
    NSString *deviceName = @"iphone";
    if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
            deviceName = @"ipad";
        }
        else {
            deviceName = @"iphone4";
        }
    }
    return deviceName;
}

That may or may not be the best way of doing it but I'd like to know how to get that into a global header so I can do something like this

NSString *deviceName = GETDEVICENAME;
A: 

#define GETDEVICENAME [whatever deviceType] maybe?

There is an issue with your function though, on 3.2 UIScreen doesn't respond to scale (at least no publicly. I wouldn't rely on that to check for iPad.

Joshua Weinberg
so cut out the UI_USER_INTERFACE_IDIOM from the if statement right? Logic is still sound if I return early
ChinaPaul
A: 

With in your project should be a file called %PROJECT%_Prefix.pch.

Any headers you include in that file will be accessible by all files in your project.

aepryus
I've got a Global header up and running but need help on the function
ChinaPaul
A: 

Got the answer that worked for me,

In a global header file Globals.h I placed

NSString* deviceType();

in Globals.m I placed a modified function

NSString* deviceType()
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        return @"ipad";
    }
    else if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    {
        return @"iphone4";
    }
    else{
        return @"iphone";

    }
}
ChinaPaul
Not clear this really does what you wan't. I think a 3GS iPhone running iOS 4.0 will show up as an iPhone4. You probably want to check respondsToSelector AND [UIScreen mainScreen].scale == 2.0
mtoy
You are correct, the 3GS does respond to scale
ChinaPaul