简体   繁体   English

在Xcode 9 / iOS 11中将应用程序图标自定义为构建阶段

[英]Customize app icon as build phase in Xcode 9/iOS 11

My app is setup to add a banner to the app icon based on the configuration being build. 我的应用程序设置为根据正在构建的配置向应用程序图标添加横幅。 It was done following these steps. 按照这些步骤完成。

https://www.raywenderlich.com/105641/change-app-icon-build-time https://www.raywenderlich.com/105641/change-app-icon-build-time

As of Xcode 9/iOS 11, this doesn't seem to work anymore. 从Xcode 9 / iOS 11开始,这似乎不再起作用了。 The images are still correctly being modified and they exist in the app bundle, but they are not being used as the app icon on the device. 图像仍然正在被修改,并且它们存在于应用程序包中,但它们不会被用作设备上的应用程序图标。

Is there any way to do this again? 有没有办法再做一次?

I dug around in this. 我在这里偷偷摸摸。 The problem is that icons (along with everything else in the asset catalog) get's packaged into a .car file, and then that is used instead of individual files. 问题是图标(以及资产目录中的其他所有内容)被打包成.car文件,然后使用它代替单个文件。 I've got no idea why the actual icon files are still being copied into the build folder (perhaps they're used for representation obscure place like the builds list in xcode or something like that). 我不知道为什么实际的图标文件仍然被复制到构建文件夹中(也许它们用于表示模糊的地方,比如xcode中的构建列表或类似的东西)。 If you want to change the icons in a build script, than you could repack the .car file using actool. 如果要更改构建脚本中的图标,则可以使用actool重新打包.car文件。 Or you would need to alter the images pre compile. 或者您需要在编译前更改图像。 Or you could potentially take apart the compilation script in xcode and find the place where it packages the .car file and then change do some pre-trickery there. 或者您可以在xcode中拆分编译脚本并找到它打包.car文件的位置,然后在那里更改一些预先欺骗。 Regardless, it's going to be quite hard to change the icons in the intermediary build folder. 无论如何,更改中间构建文件夹中的图标将变得非常困难。

Here's the solution I ended up going with. 这是我最终选择的解决方案。

  1. Remove the app icons from Assets.xcassets Assets.xcassets删除应用程序图标
  2. Create new xcassets for each configuration that needs a custom icon. 为需要自定义图标的每个配置创建新的xcassets I named them AppIcon-{CONFIGUATION}.xcassets 我将它们命名为AppIcon-{CONFIGUATION}.xcassets
  3. Create a default AppIcon.xcassets file for release builds. 为发布版本创建默认的AppIcon.xcassets文件。
  4. In the targets build settings, add the following to Excluded Source File Names 在目标构建设置中,将以下内容添加到“ Excluded Source File Names
$(SRCROOT)/$(PRODUCT_NAME)/AppIcon*.xcassets
  1. Also in the targets build settings, add the following to Included Source File Names , customizing for each configuration as needed. 此外,在目标构建设置中,将以下内容添加到“ Included Source File Names ,根据需要为每个配置进行自定义。
$(SRCROOT)/$(PRODUCT_NAME)/AppIcon-Debug.xcassets

This will cause the built application to only include the app icon assets for configuration being built. 这将导致构建的应用程序仅包含正在构建的配置的应用程序图标资产。 This can also be extended to other assets. 这也可以扩展到其他资产。

I'm having the same issue, however in iOS 10.3 they added support for modifying the app icon at runtime straight from the code. 我遇到了同样的问题,但是在iOS 10.3中,他们添加了对在运行时直接从代码修改应用程序图标的支持。 If you need to add text to the icon it might be a bit more tricky but it shouldn't be impossible. 如果你需要在图标上添加文字,可能会有点棘手,但这不应该是不可能的。

More info: 更多信息:

https://www.hackingwithswift.com/example-code/uikit/how-to-change-your-app-icon-dynamically-with-setalternateiconname https://www.hackingwithswift.com/example-code/uikit/how-to-change-your-app-icon-dynamically-with-setalternateiconname

Edit: 编辑:

I did a bit more digging and there seems to be a solution. 我做了一些挖掘,似乎有一个解决方案。 You can read more in this thread: App Icons not included in build from Xcode 9 您可以在此主题中阅读更多内容: 应用程序图标不包含在Xcode 9的构建中

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

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