iOS Development: How can I prevent an iPad from ru

2019-02-08 10:36发布

I'm diving into iOS development and I created a universal app that turned into an iPhone-only app. When it runs on the iPad, it just loads a white screen since there's no iPad code written yet. What I'd like is for it to run in "iPhone" mode on the iPad, if it somehow ends up on an iPad. I have the "Targeted Device Family" property set to "iPhone", so that should prevent it from showing up in the App Store as an iPad app, but if anyone owns both an iPad and an iPhone, then the app could end up synced to the iPad, at which point it will just load the white screen because it will try to run the app in iPad mode, which it doesn't have any code to support. In this situation, I prefer that it actually ran on the iPad, but in iPhone mode.

My questions are...

  1. When an iPad runs a universal app, how does it know to run it in "iPhone mode" or execute the iPad specific code?
  2. In a universal app, how does it know which code is iPhone and which code is iPad?
  3. How can I prevent the iPad from trying to run the iPad code and, instead, run the iPhone code?

I apologize if I sound like a total noob, but I am. Thanks so much for your wisdom!

9条回答
萌系小妹纸
2楼-- · 2019-02-08 11:00

I'm assuming what you actually want is to remove the "universal" capability, and just make it an iPhone app.

In Xcode, go to Project => Edit Project Settings => Build.

Search for universal, or 'Targeted Device Family'.

Pick iPhone.

Goodbye iPad.

查看更多
Deceive 欺骗
3楼-- · 2019-02-08 11:10

I think there is an entry in the info.plist file for each of the devices that says which main window to load. Maybe a quick and dirty solution would be to set both MainWindow-iPhone and MainWindow-iPad to the same -iPhone- main window.

查看更多
时光不老,我们不散
4楼-- · 2019-02-08 11:10

Another way to do it (with code) is:

In your App's AppDelegate (if your App was created as an Universal App) you can find the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        //iPad...

    } else {
        //iPhone and iPod Touch...

    }

    return YES;
}

There you can customize what view to show.

查看更多
Ridiculous、
5楼-- · 2019-02-08 11:13

When an iPad runs a universal app, how does it know to run it in "iPhone mode" or execute the iPad specific code?

The iPad looks for the Targeted Device Family, if the iPad is not present, then it knows it must run the app in iPhone mode.

In a universal app, how does it know which code is iPhone and which code is iPad?

When you write the code for the app, you must specify what device you are targeting if there are specific things you need to do per device. (see the code example below)

How can I prevent the iPad from trying to run the iPad code and, instead, run the iPhone code?

Do not support iPad in your Targeted Device Family. Second, in your code, do not specify that specific code needs a specific device, for example:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{
    /* run something specific for the iPad */
} 
else
{
    /* run something specific for the iPhone */
}
查看更多
Explosion°爆炸
6楼-- · 2019-02-08 11:17

Since Xcode 5, you can chose your development target devices from the Project:

From the devices section within Development Info, now you can choose:

1-iPhone 2- iPad 3- Universal

enter image description here

查看更多
手持菜刀,她持情操
7楼-- · 2019-02-08 11:21

You should NOT add this to your Info.plist file. Instead, add it to your build settings per Apple's suggestion. Specifically, use the TARGETED_DEVICE_FAMILY build setting.

If you are using storyboards, you also want to remove the UIMainStoryboardFile~ipad key from your Info.plist as it will be used regardless of your TARGETED_DEVICE_FAMILY setting.

Good luck!

查看更多
登录 后发表回答