iOS Info.plist知多少

一、什么是Info.plist

iOS中很多功能需要配置Info.plist才能实现,如设置后台运行、支持打开的文件类型等。了解Info.plist中各字段及其含义,可以访问苹果开发网站相关文档,https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009248-SW1

苹果官方对Info.plist的定义:
To provide a better experience for users, iOS and macOS rely on the presence of special metadata in each app or bundle. This metadata is used in many different ways. Some of it is displayed to the user, some of it is used internally by the system to identify your app and the document types it supports, and some of it is used by the system frameworks to facilitate the launch of apps. The way an app provides its metadata to the system is through the use of a special file called an information property list file, or Info.plistfor short.
A property list is a way to structure arbitrary data that the system can access at runtime. An information property list is a specialized type of property list that contains configuration data for a bundle. The keys and values in the file describe the various behaviors and configuration options you want applied to your bundle. An Xcode project template typically specifies an information property list file with an initial set of keys and appropriate default values. You can edit the file to change or add keys and values, as appropriate for your project.
翻译:
为了提供更好的用户体验,iOS 和 macOS 的每个app或bundle都依赖于特殊的元数据。元数据有多种用途,包括:直接向用户展示信息;系统内部用来标识你的app或其支持的文档类型;系统框架用来辅助app的加载等。这个提供给系统的元数据是通过一个名字叫 information property list file(属性列表文件)的特殊文件来实现的,这个特殊的文件简称 Info.plist
Info.plist可用来构建任意数据,这些数据在运行时是可访问的。Info.plist是每个bundle的专属配置,Info.plist文件中的keys和values描述了许多要应用于该bundle的行为以及配置选项。Xcode工程通常会自动创建一个Info.plist,并且提供许多合适的keys以及其对应的默认的values。我们可以修改或增加keys和values。

二、Info.plist长什么样

1 Info.plist的打开方式

XCode工程中打开
在这里插入图片描述
代码打开方式
在这里插入图片描述

2 每个key的含义

属性 名称 类型 描述
CFBundleDevelopmentRegion Localization native development region String 本地化相关数据,如果用户没有响应的语言资源,则默认使用这个key的value
CFBundleExecutable Executable file String 程序安装包的名称
CFBundleIdentifier Bundle indentifier String 唯一标识字符串
CFBundleInfoDictionaryVersion InfoDictionary version String Info.plist格式的版本信息,XCode会自动填写,不要修改这个值
CFBundleName Bundle name String 程序安装后在界面上显示的名称
CFBundlePackageType Bundle OS Type code String 例:BNDL、APPL、FMWK。详见官方说明
CFBundleShortVersionString Bundle versions string, short String 版本号,三位,例:1.0.0。详见官方说明
CFBundleTypeOSTypes Bundle creator OS Type code Array of strings 该字段包含了一组映射到这个类型的四字母长的类型代码。为了打开所有类型的文档,可以把它设为 “****” 。
CFBundleVersion Bundle version String 应用程序版本号,每次部署应用程序的一个新版本时,应该增加这个编号,app store审核需要用
LSRequiresIPhoneOS Application require iPhone environment Boolean 用于指示程序包是否只能运行在iPhone OS 系统上。默认是YES
UILaunchStoryboardName Launch screen interface file base name String 程序启动时的所加载的启动画面,主要成xib文件中加载,这里的值为LaunchScreen,说明从LaunchScreen.storyboard中加载
UIMainStoryboardFile Mian storyboard file base name String 程序的启动时的主画面,此文件中的视图将作为程序启动后的主画面
UISupportedInterfaceOrientations Supported interface orientations Array of strings 横竖屏设置,UIInterfaceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationLandscapeRight
UIRequiredDeviceCapabilities Required device capabilities Array of strings 应用程序运行所需的设备限制,详见官方说明

三、Info.plist中的常见的keys类别

根据功能的分类,Info.plist文件的keys大致划分为以下几类

1 Core Foundation Keys

该类的keys的特点是以CF为前缀,用以代表Core Foundation,描述了一些常用的行为项
详见官方文档
https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-SW1

2 Lanch Services Keys

该类的keys的特点是以LS为前缀。加载服务项,提供了App加载所依赖的配置,描述了App启动的方式选择。
详见官方文档
https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html#//apple_ref/doc/uid/TP40009250-SW1

常用的属性
LSBackgroundOnly
如果该字段存在并且被设为“ 1 ”,启动服务将只会运行在后台。您可以使用该字段来创建无用户界面的后台应用程序。如果您的应用程序使用了连接到窗口服务器 的高级框架,但并不需要显示出来,您也应该使用该字段。后台应用程序必须被编译成 Mach-O可执行文件。该选项不适用于CFM应用程序。
您也可以指定该字段的类型为 BooleanNumber。然而,只有 Mac OS X 10.2或以上的版本才支持这些类型的值。

LSPrefersCarbon
如果该字段被设为“ 1 ”Finder将会在显示简介面板中显示“ 在 Classic 环境中打开 ”控制选项,缺省情况下该控件未被选中。如果需要,用户可以修改这个控制选项来在 Classic环境中启动应用程序。

您也可以指定该字段的类型为 BooleanNumber。然而,只有 Mac OS X 10.2或以上的版本才支持这些类型的值。如果您在您的属性列表中加入了该字段,那么就不要同时加入 LSPrefersClassic, LSRequiresCarbon, 或LSRequiresClassic字段 。

LSPrefersClassic
如果该字段被设为“ 1 ”Finder将会在显示简介面板中显示“ 在 Classic 环境中打开 ”控制选项,缺省情况下该控件被选中。如果需要,用户可以修改这个控制选项来在 Carbon环境中启动应用程序。

您也可以指定该字段的类型为 BooleanNumber。然而,只有Mac OS X 10.2或以上的版本才支持这些类型的值。如果您在您的属性列表中加入了该字段,那么就不要同时加入LSPrefersCarbon, LSRequiresCarbon, 或 LSRequiresClassic字段。

LSRequiresCarbon
如果该字段被设为 “ 1 ”,启动服务将只在 Carbon环境中运行应用程序。如果您的应用程序不应该运行在 Classic环境中的话,可以使用该字段。

您也可以指定该字段的类型为BooleanNumber。然而,只有 Mac OS X 10.2或以上的版本才支持这些类型的值。如果您在您的属性列表中加入了该字段,那么就不要同时加入 LSPrefersCarbon, LSPrefersClassic, 或 LSRequiresClassic字段。

LSRequiresClassic
如果该字段被设为 “ 1 ”,启动服务将只在Classic环境中运行应用程序。如果您的应用程序不应该运行在Carbon兼容环境中的话,可以使用该字段。

您也可以指定该字段的类型为 BooleanNumber。然而,只有 Mac OS X 10.2或以上的版本才支持这些类型的值。如果您在您的属性列表中加入了该字段,那么就不要同时加入 LSPrefersCarbon, LSPrefersClassic, 或 LSRequiresCarbon字段。

LSUIElement
如果该字段被设为“ 1 ”,启动服务会将该应用程序作为一个用户界面组件来运行。用户界面组件不会出现在 Dock或强制退出窗口中。虽然它们通常作为后台应 用程序运行,但是如果希望的话,它们也可以在前台显示一个用户界面。点击属于用户界面组件的窗口,应用程序将会处理产生的事件。
Dock和登录窗口是两个用户界面组件应用程序。

3 Cocoa Keys

该类的keys的特点是以NS为前缀。Cocoa框架或Cocoa Touch框架依赖这些keys来标识更高级别的配置项目,如Appmain nib文件,主要类。这些key描述影响着CocoaCocoa Touch框架初始化和运行App的运行方式。
详见官方文档
https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html#//apple_ref/doc/uid/TP40009250-SW1
常见的权限配置

<!-- 相册 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>需要您的同意,APP才能访问相册</string>

<!-- 相机 -->
<key>NSCameraUsageDescription</key>
<string>需要您的同意,APP才能访问相机</string>

<!-- 麦克风 -->
<key>NSMicrophoneUsageDescription</key>
<string>需要您的同意,APP才能访问麦克风</string>

<!-- 位置 -->
<key>NSLocationUsageDescription</key>
<string>需要您的同意, APP才能访问位置</string>

<!-- 在使用期间访问位置 -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意, APP才能在使用期间访问位置</string>

<!-- 始终访问位置 -->
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意, APP才能始终访问位置</string>

<!-- 日历 -->
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意, APP才能访问日历</string>

<!-- 提醒事项 -->
<key>NSRemindersUsageDescription</key>
<string>需要您的同意, APP才能访问提醒事项</string>

<!-- 运动与健身 -->
<key>NSMotionUsageDescription</key>
<string>需要您的同意, APP才能访问运动与健身</string>

<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>需要您的同意, APP才能访问健康更新 </string>

<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>需要您的同意, APP才能访问健康分享</string>

<!-- 蓝牙 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>需要您的同意, APP才能访问蓝牙</string>

<!-- 媒体资料库 -->
<key>NSAppleMusicUsageDescription</key>
<string>需要您的同意, APP才能访问媒体资料库</string>

4 App Extension Keys

App可能需要扩展默认的plist来描述更多的信息,如定制App启动后的默认旋转方向,标识App是否支持文件共享等等。
详见官方文档
https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html#//apple_ref/doc/uid/TP40009250-SW1

四、读取Info.plist

App运行的时候,需要读取Info.plist中的信息,用以下代码可获取整个Info.plist的信息

[[NSBundle mainBundle] infoDictionary]

例,根据 key值去取 URLTypes里面对应的值

+ (NSString *)URLSchemesForkey:(NSString *)key {
    
    
    
    NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
    NSArray *urlTypes = dict[@"CFBundleURLTypes"];
    NSString *urlSchemes = nil;
    for (NSDictionary *scheme in urlTypes) {
    
    
        NSString *schemeKey = scheme[@"CFBundleURLName"];
        if ([schemeKey isEqualToString:key]) {
    
    
            urlSchemes = scheme[@"CFBundleURLSchemes"][0];
            break;
        }
    }
    return urlSchemes;
}

猜你喜欢

转载自blog.csdn.net/linxinfa/article/details/107735015