开发者

iPhone - iPad Device Type Trick in the Header File

开发者 https://www.devze.com 2023-01-09 11:35 出处:网络
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

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;


#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.


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.


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";

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消