无法获得支持的接口方向数组,为什么?

Tig*_*ing 1 iphone ipad uiinterfaceorientation ios

在这段代码中:

   NSArray *supportedOrientations = nil;
   if( iPhone ) { // bool
        supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
        // one array element
   }
   else if( iPad ) { // bool
        supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~ipad"];
        // returns nil
   }
   else {
        NSLog(@"%@:%@ device type not identified", kClassName, kMethodName);
   }
Run Code Online (Sandbox Code Playgroud)

如果设备是iPhone,则supportedOrientations具有该阵列.

如果是iPad,supportOrientations为零.

始终找到该文件,因此NSLog永远不会显示(通过调试器逐步确认).

使用文本编辑检查plist时,我看到:

    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
Run Code Online (Sandbox Code Playgroud)

有关为什么会发生这种情况的任何想法?

在iOS模拟器版本4.3中运行.

rob*_*off 11

当一个bundle加载它时infoDictionary,它会检查字典中的每个键.如果密钥是特定于设备的,则捆绑包将检查密钥是否指向当前设备.

  • 如果密钥引用当前设备,则捆绑包将删除密钥的设备特定部分.例如,在iPad上,捆绑包会更改UISupportedInterfaceOrientations~iPadUISupportedInterfaceOrientations.

  • 如果密钥引用不同的设备,则捆绑包将从字典中删除密钥.例如,在iPhone上,捆绑包会UISupportedInterfaceOrientations~iPad从字典中删除密钥.

所以你可以这样做:

NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
Run Code Online (Sandbox Code Playgroud)

这将在所有设备上做正确的事情.

你可以这样做而且它有点短:

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
Run Code Online (Sandbox Code Playgroud)