如何从 info.plist 中获取和使用 UIInterfaceOrientation 项目?

Posted

技术标签:

【中文标题】如何从 info.plist 中获取和使用 UIInterfaceOrientation 项目?【英文标题】:How to get and use UIInterfaceOrientation items from info.plist? 【发布时间】:2018-11-07 14:23:49 【问题描述】:

我想在 info.plist 中获取支持的界面方向的 item0。

为了得到这样的东西:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations 

     NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];

     return supportedOrientations[0]; 

但是我当然有一个不兼容类型错误: Incompatible pointer to integer conversion returning 'id' from a function with result type

如何解决?

【问题讨论】:

信息字典中的条目可能是一个字符串。您是否尝试过制作自己的转换函数? 【参考方案1】:

正如@Mats 在他的评论中提到的,信息字典包含支持方向的字符串值。您需要将字符串值转换为所需的UIInterfaceOrientationMask 类型。您可以使用字符串类别来执行此操作。

NSString+UIDeviceOrientation.h

static NSString *kUIInterfaceOrientationPortrait           = @"UIInterfaceOrientationPortrait";
static NSString *kUIInterfaceOrientationLandscapeLeft      = @"UIInterfaceOrientationLandscapeLeft";
static NSString *kUIInterfaceOrientationLandscapeRight     = @"UIInterfaceOrientationLandscapeRight";
static NSString *kUIInterfaceOrientationPortraitUpsideDown = @"UIInterfaceOrientationPortraitUpsideDown";

@interface NSString (UIDeviceOrientation)

- (UIInterfaceOrientationMask)deviceOrientation;

@end

NSString+UIDeviceOrientation.m

@implementation NSString (UIDeviceOrientation)

- (UIInterfaceOrientationMask)deviceOrientation 
    UIInterfaceOrientationMask mask = UIInterfaceOrientationMaskAll;

    if ([self isEqualToString:kUIInterfaceOrientationPortrait]) 
        mask = UIInterfaceOrientationMaskPortrait;
    
    else if ([self isEqualToString:kUIInterfaceOrientationLandscapeLeft]) 
        mask = UIInterfaceOrientationMaskLandscapeLeft;
    
    else if ([self isEqualToString:kUIInterfaceOrientationLandscapeRight]) 
        mask = UIInterfaceOrientationMaskLandscapeRight;
    
    else if ([self isEqualToString:kUIInterfaceOrientationPortraitUpsideDown]) 
        mask = UIInterfaceOrientationMaskPortraitUpsideDown;
    

    return mask;

Swift 版本

extension String 
private var kUIInterfaceOrientationPortrait: String 
    return "UIInterfaceOrientationPortrait"


private var kUIInterfaceOrientationLandscapeLeft: String 
    return "UIInterfaceOrientationLandscapeLeft"


private var kUIInterfaceOrientationLandscapeRight: String 
    return "UIInterfaceOrientationLandscapeRight"


private var kUIInterfaceOrientationPortraitUpsideDown: String 
    return "UIInterfaceOrientationPortraitUpsideDown"


public var deviceOrientation: UIInterfaceOrientationMask 

    switch self 
    case kUIInterfaceOrientationPortrait:
        return .portrait

    case kUIInterfaceOrientationLandscapeLeft:
        return .landscapeLeft

    case kUIInterfaceOrientationLandscapeRight:
        return .landscapeRight

    case kUIInterfaceOrientationPortraitUpsideDown:
        return .portraitUpsideDown

    default:
        return .all
    


【讨论】:

以上是关于如何从 info.plist 中获取和使用 UIInterfaceOrientation 项目?的主要内容,如果未能解决你的问题,请参考以下文章

如何将 Info.plist 文件从 mac os 项目转换为 iphone 项目?

如何获取 INFOPLIST_FILE 名称 iOS

从 Info.plist 读取版本

UIUserInterfaceStyle 键在 Xcode 12 的 Info.plist 中不可用,如何从 Info.plist 禁用 Xcode 12 中的暗模式?

从 iOS 的 UITests 中读取 Info.plist?

获取 Info.plist 的所有 CFBundleTypeExtensions