Xamarin.iOS 中的备用应用图标

本文介绍如何在 Xamarin.iOS 中使用备用应用图标。

Apple 在 iOS 10.3 中添加了多项增强功能,允许应用管理其图标:

  • ApplicationIconBadgeNumber - 获取或设置 Springboard 中应用图标的徽章。
  • SupportsAlternateIcons - 如果为 true,则应用有一组备用图标。
  • AlternateIconName - 返回当前选择的备用图标的名称,如果使用主图标,则返回 null
  • SetAlternameIconName - 使用此方法将应用的图标切换为给定的备用图标。

A sample alert when an app changes its icon

将备用图标添加到 Xamarin.iOS 项目

若要允许应用切换到备用图标,需要将图标图像集合包含在 Xamarin.iOS 应用项目中。 无法使用典型 Assets.xcassets 方法将这些图像添加到项目中,必须直接将其添加到“资源”文件夹中

请执行以下操作:

  1. 在文件夹中选择所需的图标图像,全选并将其拖到解决方案资源管理器中的“资源”文件夹中

    Select the icons images from a folder

  2. 出现提示时,选择“复制”,对所有选定的文件使用相同的操作,然后单击“确定”按钮

    The Add File to Folder dialog box

  3. 完成后,“资源”文件夹应如下所示

    The Resources folder should look like this

修改 Info.plist 文件

将所需图像添加到“资源”文件夹后,需要将 CFBundleAlternateIcons 键添加到项目的 Info.plist 文件中。 此键将定义新图标的名称和构成它的图像。

请执行以下操作:

  1. 在“解决方案资源管理器”中,双击“Info.plist”文件,将其打开进行编辑。
  2. 切换到“源”视图
  3. 添加 Bundle Icons 键并将“类型”设置为“字典”
  4. 添加 CFBundleAlternateIcons 键并将“类型”设置为“字典”
  5. 添加 AppIcon2 键并将“类型”设置为“字典”。 这是新的备用应用图标集的名称。
  6. 添加 CFBundleIconFiles 键并将“类型”设置为“数组”
  7. 为每个图标文件添加一个新字符串到 CFBundleIconFiles 数组中,省略扩展名和 @2x@3x 等后缀(例如 100_icon)。 对构成备用图标集的每个文件重复此步骤。
  8. AppIcon2 字典添加一个 UIPrerenderedIcon 键,将“类型”设置为“布尔”并将值设置为“否”
  9. 保存对文件所做的更改。

完成后,生成的 Info.plist 文件应如下所示

The completed Info.plist file

如果在文本编辑器中打开,则如下所示:

<key>CFBundleIcons</key>
<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>AppIcon2</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>100_icon</string>
                <string>114_icon</string>
                <string>120_icon</string>
                <string>144_icon</string>
                <string>152_icon</string>
                <string>167_icon</string>
                <string>180_icon</string>
                <string>29_icon</string>
                <string>40_icon</string>
                <string>50_icon</string>
                <string>512_icon</string>
                <string>57_icon</string>
                <string>58_icon</string>
                <string>72_icon</string>
                <string>76_icon</string>
                <string>80_icon</string>
                <string>87_icon</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
    </dict>
</dict>

管理应用的图标

通过正确配置 Xamarin.iOS 项目和 Info.plist 文件中包含的图标图像,开发人员可以使用 iOS 10.3 中增加的众多新功能之一来控制应用的图标

UIApplication 类的 SupportsAlternateIcons 属性允许开发人员查看应用是否支持备用图标。 例如:

// Can the app select a different icon?
PrimaryIconButton.Enabled = UIApplication.SharedApplication.SupportsAlternateIcons;
AlternateIconButton.Enabled = UIApplication.SharedApplication.SupportsAlternateIcons;

UIApplication 类的 ApplicationIconBadgeNumber 属性允许开发人员获取或设置 Springboard 中应用图标的当前锁屏提醒数。 默认值为零 (0)。 例如:

// Set the badge number to 1
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 1;

UIApplication 类的 AlternateIconName 属性允许开发人员获取当前选定的备用应用图标的名称,或者如果应用使用主图标,则返回 null。 例如:

// Get the name of the currently selected alternate
// icon set
var name = UIApplication.SharedApplication.AlternateIconName;

if (name != null ) {
    // Do something with the name
}

UIApplication 类的 SetAlternameIconName 属性允许开发人员更改应用图标。 传递要选择的图标的名称或 null 以返回主图标。 例如:

partial void UsePrimaryIcon (Foundation.NSObject sender)
{
    UIApplication.SharedApplication.SetAlternateIconName (null, (err) => {
        Console.WriteLine ("Set Primary Icon: {0}", err);
    });
}

partial void UseAlternateIcon (Foundation.NSObject sender)
{
    UIApplication.SharedApplication.SetAlternateIconName ("AppIcon2", (err) => {
        Console.WriteLine ("Set Alternate Icon: {0}", err);
    });
}

当应用运行时,用户选择备用图标时,将显示如下所示的警报:

A sample alert when an app changes its icon

如果用户切换回主图标,将显示如下所示的警报:

A sample alert when an app changes to the primary icon

总结

本文介绍了如何将备用应用图标添加到 Xamarin.iOS 项目,并在应用内使用这些图标。