How to detect my iPhone app is being run on an iPad

拟墨画扇 提交于 2019-12-01 00:26:47

you shouldn't be able to tell the difference, if its an iPhone app, then as far as it can tell it is running on an iPhone. if you want to target an iPad, then you need to build it for an iPad target.

If you are looking to make custom code (most likely custom UI related methods) for the iPad only then you can use (as Apple directs) the UI_USER_INTERFACE_IDIOM() method that exists in iOS 3.2 and later

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    // The device is an iPad running iPhone 3.2 or later.
}
else
{
    // The device is an iPhone or iPod touch.

}

You can read more here http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BuildTimeConfiguration/BuildTimeConfiguration.html

This is the Apple recommended method

If the app is an iPhone app running in the emulator mode on an iPad, it will have a userInterfaceIdiom of Phone, but a model type of iPad. You can check this with the following code:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
    [[[UIDevice currentDevice] model] hasPrefix:@"iPad"]) {
    // This app is an iPhone app running on an iPad
}

Look up in the documentation, UIDevice:

For instance something like: NSString *system = [[UIDevice currentDevice] systemName];

Then by using [system isEqualToString:@"iPad"] whether its an ipad or not.

UIDevice is a very nice class, it also has stuff like multiTaskingSupported, systemVersion etc. gotta love UIKit ;)

i think it is that:

// Set hello to "Hello, <device or simulator>"!

if TARGET_IPHONE_SIMULATOR

NSString *hello = @"Hello, iOS Simulator!";

else

NSString *hello = @"Hello, iOS device!";

endif

the link apple doc

about

This actually will only tell you if the app is being run in a simulated environment, or on an actual device, and has no influence on whether the platform is iPad or iPhone.

In fact it says at compile time the target of the platform you are compiling for, thus before run you know and do the necessary for take care of something specific.

For example I have diferent URL for developing (running on simulator) and for production usage, so I do some like

#if TARGET_IPHONE_SIMULATOR
#define URL @"http://192.x.x.x/request"
#else
#define URL @"http://example.com/request"
#endif
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!