简体   繁体   English

iOS开发:如何防止iPad在iPad模式下运行通用应用程序?

[英]iOS Development: How can I prevent an iPad from running a universal app in iPad mode?

I'm diving into iOS development and I created a universal app that turned into an iPhone-only app. 我正在深入iOS开发,我创建了一个通用应用程序,变成了一个仅限iPhone的应用程序。 When it runs on the iPad, it just loads a white screen since there's no iPad code written yet. 当它在iPad上运行时,它只会加载一个白色屏幕,因为还没有写入iPad代码。 What I'd like is for it to run in "iPhone" mode on the iPad, if it somehow ends up on an iPad. 我想要的是它在iPad上以“iPhone”模式运行,如果它以某种方式最终在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. 我将“目标设备系列”属性设置为“iPhone”,这样可以防止它作为iPad应用程序在App Store中显示,但如果有人同时拥有iPad和iPhone,那么应用程序最终可能会同步在iPad上,它只会加载白色屏幕,因为它会尝试在iPad模式下运行应用程序,它没有任何支持的代码。 In this situation, I prefer that it actually ran on the iPad, but in iPhone mode. 在这种情况下,我更喜欢它实际上在iPad上运行,但在iPhone模式下。

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? 当iPad运行通用应用程序时,如何知道在“iPhone模式”下运行它还是执行iPad特定代码?
  2. In a universal app, how does it know which code is iPhone and which code is iPad? 在通用应用程序中,它如何知道哪些代码是iPhone以及哪些代码是iPad?
  3. How can I prevent the iPad from trying to run the iPad code and, instead, run the iPhone code? 如何防止iPad试图运行iPad代码,而是运行iPhone代码?

I apologize if I sound like a total noob, but I am. 如果我听起来像一个总菜鸟,我道歉,但我是。 Thanks so much for your wisdom! 非常感谢你的智慧!

  1. The iPad looks into the application's Info.plist, for the UIDeviceFamily key, which is an array. iPad会查看应用程序的Info.plist,其中包含UIDeviceFamily键,它是一个数组。 The value '1' indicates iPhone/iPod Touch, '2' indicates 'iPad'. 值“1”表示iPhone / iPod Touch,“2”表示“iPad”。 If there's a '1' but no '2' then you get the simulated iPhone environment. 如果有一个'1'但没有'2'那么你会得到模拟的iPhone环境。 This value is written to the Info.plist automatically as a result of your 'Targeted Device Family'. 由于您的“目标设备系列”,此值会自动写入Info.plist。 If you think you've set it to iPhone but are still getting an iPad build, check you didn't just set it for one build configuration. 如果您认为自己已将其设置为iPhone但仍在进行iPad构建,请检查您是否只为一个构建配置设置了它。 Check the Info.plist within your produced app bundle if you want to be really confident. 如果您想要真正自信,请查看生产的应用包中的Info.plist。
  2. There's only one binary, which you write to launch correctly on either device. 只有一个二进制文件可以在任一设备上正确启动。
  3. Just don't target the iPad. 只是不要瞄准iPad。

I'm assuming what you actually want is to remove the "universal" capability, and just make it an iPhone app. 我假设您真正想要的是删除“通用”功能,并将其设为iPhone应用程序。

In Xcode, go to Project => Edit Project Settings => Build. 在Xcode中,转到Project => Edit Project Settings => Build。

Search for universal, or 'Targeted Device Family'. 搜索通用或“目标设备系列”。

Pick iPhone. 选择iPhone。

Goodbye iPad. 再见iPad。

When an iPad runs a universal app, how does it know to run it in "iPhone mode" or execute the iPad specific code? 当iPad运行通用应用程序时,如何知道在“iPhone模式”下运行它还是执行iPad特定代码?

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. iPad寻找Targeted Device Family ,如果iPad不存在,那么它知道它必须在iPhone模式下运行应用程序。

In a universal app, how does it know which code is iPhone and which code is iPad? 在通用应用程序中,它如何知道哪些代码是iPhone以及哪些代码是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? 如何防止iPad试图运行iPad代码,而是运行iPhone代码?

Do not support iPad in your Targeted Device Family . 不要在Targeted Device Family支持iPad。 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 */
}

You should NOT add this to your Info.plist file. 您不应将此添加到Info.plist文件中。 Instead, add it to your build settings per Apple's suggestion . 相反,根据Apple的建议将其添加到您的构建设置中。 Specifically, use the TARGETED_DEVICE_FAMILY build setting. 具体来说,使用TARGETED_DEVICE_FAMILY构建设置。

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. 如果您使用的是故事板,则还需要从Info.plist中删除UIMainStoryboardFile~ipad密钥,因为无论您的TARGETED_DEVICE_FAMILY设置如何,都会使用它。

Good luck! 祝好运!

  1. If you build an universal app, it will use your iPad code. 如果您构建通用应用程序,它将使用您的iPad代码。 It is not possible to run a universal app in "iPhone Mode". 无法在“iPhone模式”下运行通用应用程序。 Apple will check that you have followed the iPad design specifications. Apple将检查您是否遵循了iPad设计规范。

  2. In a universal app, there are two app-delegates: AppDelegate_iPhone.h and AppDelegate_iPad.h 在通用应用程序中,有两个app-delegate:AppDelegate_iPhone.h和AppDelegate_iPad.h

  3. You can add your iPhone code in the AppDelegate_iPad, but Apple will not be pleased. 您可以在AppDelegate_iPad中添加iPhone代码,但Apple不会高兴。

Since Xcode 5 , you can chose your development target devices from the Project: Xcode 5 ,您可以从项目中选择开发目标设备:

From the devices section within Development Info , now you can choose: Development Info的设备部分,现在您可以选择:

1-iPhone 2- iPad 3- Universal 1-iPhone 2- iPad 3- Universal

在此输入图像描述

I think that something is wrong with your configuration, because if you target the code for iPhone only Device, the app will bu runnable on an iPad with the screen that was designed for iPhone (so, reduced, with the possibility to x2). 我认为你的配置出了问题,因为如果你只针对iPhone的设备定位代码,应用程序就可以在iPad上运行,屏幕是专为iPhone设计的(因此,减少了,有可能是x2)。

  • the proposals from the AppStore (iPhone/iPad/both) depend on the user's preferences AppStore(iPhone / iPad /两者)的提议取决于用户的偏好
  • you can experiment it with the Simulator (and choose iPad as the Device) 您可以使用模拟器进行实验(并选择iPad作为设备)
  • the code is the same for iPad/iPhone ! iPad / iPhone的代码是一样的! ... unless you use [[UIDevice currentDevice] userInterfaceIdiom] mentioned supra... ...除非你使用上面提到的[[UIDevice currentDevice] userInterfaceIdiom] ...

I think there is an entry in the info.plist file for each of the devices that says which main window to load. 我认为info.plist文件中有一个条目,用于说明要加载哪个主窗口的每个设备。 Maybe a quick and dirty solution would be to set both MainWindow-iPhone and MainWindow-iPad to the same -iPhone- main window. 也许一个快速而肮脏的解决方案是将MainWindow-iPhone和MainWindow-iPad设置到同一个-iPhone-主窗口。

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: 在App的AppDelegate中(如果您的应用程序是作为通用应用程序创建的),您可以找到以下代码:

- (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. 在那里,您可以自定义要显示的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM