iOS:通过 Xcode Target 构建资源包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8458953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Build a Resource Bundle via Xcode Target
提问by cschuff
I want to create a Resource Bundle from resource files of my static library via a xcode target. I saw Three20 having a .bundle file one need to import.
我想通过 xcode 目标从我的静态库的资源文件创建一个资源包。我看到 Three20 有一个需要导入的 .bundle 文件。
How can I do that?
我怎样才能做到这一点?
回答by stevendesu
Important Disclaimer:
重要免责声明:
Apple does not endorse this solution. When I spoke with a developer at Apple who works on Xcode they informed me that resource bundles were not supportedby iOS, and they were surprised that this worked at all.
Apple 不认可此解决方案。当我与一位在 Xcode 上工作的 Apple 开发人员交谈时,他们告诉我iOS不支持资源包,他们很惊讶这竟然有效。
This solution has worked for me on iOS 9 through 13, both phone and tablet. However it's possible that a future update to iOS may break this. Just be aware of that.
这个解决方案在 iOS 9 到 13 上对我有用,包括手机和平板电脑。但是,未来对 iOS 的更新可能会破坏这一点。请注意这一点。
Now on to the answer:
现在来回答:
I know it's 8 years late, but since I stumbled across this question there's a good chance someone else will, so I thought I'd provide a helpful answer.
我知道已经晚了 8 年,但是由于我偶然发现了这个问题,因此其他人很有可能会这样做,所以我想我会提供一个有用的答案。
Step 1. Create a new Target
步骤 1. 创建一个新目标
- Make sure you have your project selected in the Project Navigator
- Click on the
+
at the bottom of the Targets list
- 确保您在项目导航器中选择了您的项目
- 单击
+
目标列表底部的
Step 2. Create a macOS Bundle
第 2 步。创建一个 macOS 包
We'll convert this to an iOS Bundle in step 3
我们将在步骤 3 中将其转换为 iOS Bundle
Step 2 (cont). Create a macOS Bundle
第 2 步(续)。创建 macOS 捆绑包
- Name your bundle
- Click "Finish"
- 命名您的捆绑包
- 点击“完成”
Step 3. Change the Base SDK from macOS to iOS
步骤 3. 将 Base SDK 从 macOS 更改为 iOS
- Make sure you have the new resource bundle target selected
- Go to Build Settings
- Look for
Base SDK
and change this value frommacOS
toiOS
- 确保您选择了新的资源包目标
- 转到构建设置
- 查找
Base SDK
并将此值从 更改macOS
为iOS
At this point, you're technically done with your question (creating a Resource Bundle target for iOS), but there's some configuration needed to get it working correctly
在这一点上,您在技术上已经完成了您的问题(为 iOS 创建资源包目标),但是需要一些配置才能使其正常工作
Step 4. Replace iOS Bundle Resources
步骤 4. 替换 iOS 捆绑资源
- Make sure you have your app target selected (notthe resource bundle)
- Go to Build Phases
- Select all resources
- Press the
-
button to remove them
- 确保您选择了应用目标(而不是资源包)
- 转到构建阶段
- 选择所有资源
- 按下
-
按钮将它们移除
Step 4 (cont). Replace iOS Bundle Resources
第 4 步(续)。替换 iOS 捆绑资源
- Press the
+
button to add a new bundle resource to the iOS app (not pictured) - Select your bundle product
- Click "Add"
- 按下
+
按钮向 iOS 应用程序添加新的捆绑资源(未图示) - 选择您的捆绑产品
- 点击“添加”
Step 5. Set up Dependencies
步骤 5. 设置依赖项
- Open the Dependenciessection (still under Build Phases and still within your app, not the resource bundle)
- Click the
+
button to add a new dependency
- 打开Dependencies部分(仍在 Build Phases 下并且仍在您的应用程序中,而不是资源包中)
- 单击
+
按钮添加新的依赖项
Step 5 (cont). Set up Dependencies
第 5 步(续)。设置依赖
- Select the resource bundle project
- Click "Add"
- 选择资源包项目
- 点击“添加”
Now you're done!
现在你完成了!
With all of those steps completed, you will be building a resource bundle compatible with iOS and embedding it inside of your application to be loaded. It's not as straight-forward and a lot of articles may say this is unsupported, but I've used this feature in a published app and it works perfectly
完成所有这些步骤后,您将构建一个与 iOS 兼容的资源包并将其嵌入到要加载的应用程序中。这不是那么简单,很多文章可能会说这是不受支持的,但我已经在已发布的应用程序中使用了此功能,并且效果很好
Just remember that if you want to add any resources to your app now (PNGs, XIB files, etc) you need to add them to the bundleand not the app, like so:
请记住,如果您现在想向您的应用程序添加任何资源(PNG、XIB 文件等),您需要将它们添加到包而不是应用程序,如下所示:
Important Gotcha
重要问题
After you've set this up, check out your resource bundle's build settings one more time. Look for the setting titled "Versioning System". This value must beset to None
. If this is set to Apple Generic
, you willrun into issues when you try to push to the app store!
设置好之后,再检查一次资源包的构建设置。查找标题为“版本控制系统”的设置。该值必须设置为None
。如果设置为Apple Generic
,当您尝试推送到应用商店时会遇到问题!
Apple's versioning system will create a *_vers.c
file, compile it, and embed this binary into your bundle. As this is not a valid CFBundleExecutable, it will cause your app to be rejected with error ITMS-90171
Apple 的版本控制系统将创建一个*_vers.c
文件,编译它,然后将此二进制文件嵌入到您的包中。由于这不是有效的 CFBundleExecutable,它会导致您的应用程序被拒绝并显示错误 ITMS-90171
回答by Don
A bundle is just a directory with a special structure and a .bundle extension. You can read more about the specifics in the documentation.
捆绑包只是一个具有特殊结构和 .bundle 扩展名的目录。您可以在文档中阅读更多关于细节的信息。
Edit:
编辑:
The important part is that a bundle is just a normal folder named with a special extension. For instance, if you had a folder named MyBundle.bundle and an image inside named image.png, you can load it with [UIImage imageNamed:@"MyBundle.bundle/image.png"];
Thus, there's no need to build it via an Xcode target. In fact, it's not available on iOS projects. You could have a Copy Files build phase in your project, or a shell script.
重要的是,bundle 只是一个带有特殊扩展名的普通文件夹。例如,如果您有一个名为 MyBundle.bundle 的文件夹和一个名为 image.png 的图像,您可以使用[UIImage imageNamed:@"MyBundle.bundle/image.png"];
因此加载它,无需通过 Xcode 目标构建它。实际上,它在 iOS 项目中不可用。您可以在项目或 shell 脚本中使用 Copy Files 构建阶段。
This is simple, and powerful. For example, if you conform to the special structure listed at the docs for frameworks, you can build static libs into Frameworks using a simple method, shell scripts or ant tasks or rake files. As long as the output directory conforms to the structure and is named Something.framework, you can use it as a Framework, even on iOS.
这很简单,也很强大。例如,如果您符合框架文档中列出的特殊结构,您可以使用简单的方法、shell 脚本或 ant 任务或 rake 文件将静态库构建到框架中。只要输出目录符合结构,命名为Something.framework,就可以作为Framework使用,即使在iOS上。