ios编程修改应用图标



(Swift Programming)

Apple introduced in iOS 10.3, a new feature that make possible for developers to offer App Icon customization to their users with predefined additional icons.

Apple在iOS 10.3中引入了一项新功能,使开发人员可以通过预定义的其他图标向其用户提供App Icon定制

Here are the 4 steps to add this cool feature to your App. Let’s get started.

以下是将此炫酷功能添加到您的App的4个步骤 。 让我们开始吧。

(Step 1: Design your App Icons)

Design your alternative App Icons and export them in two sizes:

设计替代的应用程序图标并以两种尺寸导出它们:

  • 120px (60px @2x)
    120px(60px @ 2x)
  • 180px (60px @3x)
    180px(60px @ 3x)

Add the icons to your project under a new directory App Icons. Note that the alternate icons files must to be in the Project directory, not in the Assets directory.

将图标添加到新目录App Icons下的项目中。 请注意,备用图标文件必须位于Project目录中 ,而不要位于Assets目录中。





App your app icons file in the project directory 在项目目录中应用您的应用程序图标文件

(Step 2: Register your new Icons in the Info.plist file)

First, add a new CFBundleIcons entry (Icon files (iOS 5)), then add another entry CFBundleAlternateIcons.

首先,添加一个新的CFBundleIcons条目(图标文件(iOS 5)),然后添加另一个条目CFBundleAlternateIcons


CFBundleAlternateIcons entry in Info.plist

Info.plist中的CFBundleAlternateIcons条目

For each alternate icon, add a new entry in the infos.plist file, under CFBundleAlternateIcons. The name of the entry is the name of the icon which will be used later in your Xcode project, and the string value of the item is the name of the icon file that you added in the project at Step 1.

对于每个备用图标,请在infos.plist文件中CFBundleAlternateIcons下添加一个新条目。 条目的名称是图标的名称,该名称将在以后的Xcode项目中使用,该项目的字符串值是您在步骤1中添加到项目中的图标文件的名称。


App icons entries in Info.plist

Info.plist中的应用程序图标条目

Once you’ve added all your icons in Info.plist, your alternate icons are ready to be used in your App.

将所有图标添加到Info.plist中后,备用图标即可在您的应用中使用。

(Step 3: The App Icon Manager)

The Apple API to switch App Icons is quite simple and consists of 3 var/functions:

用于切换应用程序图标的Apple API非常简单,由3个var /功能组成:

var supportsAlternateIcons: Bool { get }open func setAlternateIconName(_ alternateIconName: String?, completionHandler: ((Error?) -> Void)? = nil)open var alternateIconName: String? { get }

As per the Apple Documentation, supportsAlternateIcons will be true when the system allows you to change the icon of then App, or false otherwise.

根据Apple文档, supportsAlternateIcons 将是true 当系统允许您更改App的图标时,否则返回false

The setAlternateIconName method is used to change the App Icon to its primary icon or to one of its alternate icons. If alternateIconName is nil then the default App Icon will be used.

setAlternateIconName方法用于将“应用程序图标”更改为其主要图标或其他图标之一。 如果alternateIconNamenil则将使用默认的应用程序图标。

Finally, alternateIconName returns the name of the alternate icon currently used, or nil if the default icon is used.

最后, alternateIconName返回当前使用的替代图标的名称;如果使用默认图标,则返回nil

To handle icon changes easily, we will create an Icon Manager to interact with Apple APIs. First, create an enum containing each of your alternate App Icons.

为了轻松处理图标更改,我们将创建一个图标管理器来与Apple API进行交互。 首先, 创建一个包含每个备用应用程序图标的枚举

BMAppIconManager.swift — BMAppIcon enum. BMAppIconManager.swift — BMAppIcon枚举。

Now let’s add the file name of each of our icon in the enum, and a preview icon that will be displayed in our App UI. In our enum, classic is the default app icon. That’s why the file name for it will be nil. For more information on why the file name is nil you can check the alternateIconName description on Apple documentation.

现在,让我们在枚举中添加每个图标的文件名 ,以及将在我们的应用程序用户界面中显示的预览图标 。 在我们的枚举中, classic是默认的应用程序图标。 这就是为什么其文件名将为nil 。 有关为什么文件名为nil的更多信息,请查看Apple文档上的AlternativeIconName描述。

BMAppIconManager.swift — BMAppIcon enum. BMAppIconManager.swift — BMAppIcon枚举。

Now that we have our enum, let’s create an AppIconManger class with two functions: one to retrieve the current App Icon, and one to update it.

现在我们有了枚举,让我们创建一个具有两个功能的AppIconManger类:一个用于检索当前的应用程序图标,另一个用于更新它。

BMAppIconManager.swift — Update App Icon. BMAppIconManager.swif t-更新应用程序图标。

(Step 4: Use your App Icon Manager in your App)

Final step, to update the current App Icon, just call the setIcon function you previously defined and pass the new icon you want to set as parameter.

最后一步,要更新当前的应用程序图标,只需调用先前定义的setIcon函数,然后传递要设置为参数的新图标即可。

With the current variable you can know which one of the alternate -or default- icon is currently used, so you can adapt your interface accordingly if needed.

使用current变量,您可以知道当前使用了替代图标(或default-)中的哪个图标,因此可以根据需要调整界面。

Build and run your App. And here is the result!

生成并运行您的应用程序。 结果就是这里!


Choose your app icon from the App interface. 从应用程序界面中选择您的应用程序图标。


As you might have see when you switch the App Icon, iOS displays an AlertView to the user stating the icon has changed.

正如您在切换应用程序图标时所看到的,iOS向用户显示AlertView,说明图标已更改。

If the Alternate App Icon is a good feature to add new customization possibilities to your App, it’s currently not flexible enough to make dynamic icons like the default weather or clock App icons.

如果“ 备用应用程序图标”是为您的应用程序添加新的自定义功能的良好功能,则它目前还不够灵活,无法制作动态图标,例如默认天气或时钟应用程序图标。

Beside that limitation, there is a lot of integration possibilities in your apps. You can imagine different ways to offer additional icons to your users, by providing some exclusive icons to your Premium users for example, or by unlocking icons after an objective, for a specific celebration, during a period of the year, etc.

除此限制外,您的应用程序中还有很多集成的可能性。 您可以想像通过其他方式为用户提供其他图标的方式,例如为高级用户提供一些专有图标,或者在目标之后,一年中的特定庆祝活动等期间解锁图标。

(Recap)

  1. Create your icon files in the required sizes.
  2. Add them in your project and register them in Info.plist.
  3. Encapsulate Apple APIs with an App Icon Manager.
  4. Use your App Icon Manager to update the App Icon from your App UI.

(You may also like this article:)


翻译自: https://medium.com/swlh/change-programmatically-your-app-icon-ios-swift-guide-3a8ee4f01649

ios编程修改应用图标