简体   繁体   English

iOS:通过 Xcode Target 构建资源包

[英]iOS: Build a Resource Bundle via Xcode Target

I want to create a Resource Bundle from resource files of my static library via a xcode target.我想通过 xcode 目标从我的静态库的资源文件创建一个资源包。 I saw Three20 having a .bundle file one need to import.我看到 Three20 有一个需要导入的 .bundle 文件。

How can I do that?我怎样才能做到这一点?

Important Disclaimer:重要免责声明:

Apple does not endorse this solution. Apple 不认可此解决方案。 When I spoke with a developer at Apple who works on Xcode they informed me that resource bundles were not supported by iOS, and they were surprised that this worked at all.当我与一位在 Xcode 上工作的 Apple 开发人员交谈时,他们告诉我 iOS不支持资源包,他们很惊讶这竟然有效。

This solution has worked for me on iOS 9 through 13, both phone and tablet.这个解决方案在 iOS 9 到 13 上对我有用,包括手机和平板电脑。 However it's possible that a future update to iOS may break this.但是,未来对 iOS 的更新可能会破坏这一点。 Just be aware of that.请注意这一点。

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).第 2 步(续)。 Create a macOS Bundle创建 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 from macOS to iOS查找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 ( not the resource bundle)确保您选择了应用程序目标(而不是资源包)
  • Go to Build Phases转到构建阶段
  • Select all resources选择所有资源
  • Press the - button to remove them-按钮删除它们

Step 4 (cont).第 4 步(续)。 Replace iOS Bundle Resources替换 iOS 捆绑资源

在此处输入图片说明

  • Press the + button to add a new bundle resource to the iOS app (not pictured)+按钮向 iOS 应用程序添加新的捆绑资源(未图示)
  • Select your bundle product选择您的捆绑产品
  • Click "Add"点击“添加”

Step 5. Set up Dependencies步骤 5. 设置依赖项

在此处输入图片说明

  • Open the Dependencies section (still under Build Phases and still within your app, not the resource bundle)打开Dependencies部分(仍在 Build Phases 下并且仍在您的应用程序中,而不是资源包中)
  • Click the + button to add a new dependency单击+按钮添加新的依赖项

Step 5 (cont).第 5 步(续)。 Set up Dependencies设置依赖

在此处输入图片说明

  • 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.完成所有这些步骤后,您将构建一个与 iOS 兼容的资源包并将其嵌入到要加载的应用程序中。 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这不是那么简单,很多文章可能会说这是不受支持的,但我已经在已发布的应用程序中使用了此功能,并且效果很好

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 bundle and 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 be set to None .此值必须设置为None If this is set to Apple Generic , you will run into issues when you try to push to the app store!如果将其设置为Apple Generic在尝试推送到应用商店时遇到问题!

Apple's versioning system will create a *_vers.c file, compile it, and embed this binary into your bundle. Apple 的版本控制系统将创建一个*_vers.c文件,编译它,然后将此二进制文件嵌入到您的包中。 As this is not a valid CFBundleExecutable, it will cause your app to be rejected with error ITMS-90171由于这不是有效的 CFBundleExecutable,它会导致您的应用程序被拒绝并出现错误 ITMS-90171

在此处输入图片说明

A bundle is just a directory with a special structure and a .bundle extension.捆绑包只是一个具有特殊结构和 .bundle 扩展名的目录。 You can read more about the specifics in the documentation .您可以在文档中阅读更多关于细节的信息

Edit:编辑:

The important part is that a bundle is just a normal folder named with a special extension.重要的是,bundle 只是一个带有特殊扩展名的普通文件夹。 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"];例如,如果你有一个名为 MyBundle.bundle 的文件夹和一个名为 image.png 的图像,你可以用[UIImage imageNamed:@"MyBundle.bundle/image.png"];加载它[UIImage imageNamed:@"MyBundle.bundle/image.png"]; Thus, there's no need to build it via an Xcode target.因此,无需通过 Xcode 目标构建它。 In fact, it's not available on iOS projects.实际上,它不适用于 iOS 项目。 You could have a Copy Files build phase in your project, or a shell script.您可以在项目或 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.例如,如果您符合框架文档中列出的特殊结构,您可以使用简单的方法、shell 脚本或 ant 任务或 rake 文件将静态库构建到框架中。 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.只要输出目录符合结构并且命名为Something.framework,你就可以将其作为Framework使用,即使在iOS上也是如此。

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

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