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 方法新增至專案,它們必須直接新增至 Resources 資料夾。

執行下列操作:

  1. 選取資料夾中的必要圖示影像,選取所有影像,並將其拖曳至 方案總管 中的 Resources 資料夾:

    Select the icons images from a folder

  2. 出現提示時,請選取 [複製],針對所有選取的檔案使用相同的動作,然後按兩下 [確定] 按鈕:

    The Add File to Folder dialog box

  3. 完成 時,Resources 資料夾看起來應該如下所示:

    The Resources folder should look like this

修改 Info.plist 檔案

將必要的影像新增至 Resources 資料夾後, 必須將 CFBundleAlternateIcons 機碼新增至專案的 Info.plist 檔案。 此索引鍵會定義新圖示的名稱,以及組成它的影像。

執行下列操作:

  1. 在 [方案總管]中,按兩下 [Info.plist] 檔案以開啟它進行編輯。
  2. 切換至 [來源] 檢視。
  3. 新增套件 組合圖示 索引鍵,並將 [ 類型 ] 設定為 [ 字典]。
  4. CFBundleAlternateIcons新增索引鍵,並將 [類型] 設定為 [字典]。
  5. AppIcon2新增索引鍵,並將 [類型] 設定為 [字典]。 這會是新的替代應用程式圖示集的名稱。
  6. CFBundleIconFiles新增索引鍵並將 Type 設定Array
  7. 將新的字串新增至每個圖示檔案的 CFBundleIconFiles 數位,以省去擴展名和 @2x@3x等後綴 (範例 100_icon)。 針對組成替代圖示集的每個檔案重複此步驟。
  8. UIPrerenderedIcon將索引鍵新增至AppIcon2字典,將 Type 設定為布爾值,並將值設定為 No
  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 的許多新功能之一來控制應用程式的圖示。

類別 SupportsAlternateIconsUIApplication 屬性可讓開發人員查看應用程式是否支援替代圖示。 例如:

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

類別 ApplicationIconBadgeNumberUIApplication 屬性可讓開發人員取得或設定 Springboard 中應用程式圖示的目前徽章編號。 預設值為零 (0)。 例如:

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

類別 AlternateIconNameUIApplication 屬性可讓開發人員取得目前選取的替代應用程式圖示名稱,或者如果應用程式使用主要圖示,則會傳 null 回該圖示。 例如:

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

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

類別 SetAlternameIconNameUIApplication 屬性可讓開發人員變更應用程式圖示。 傳遞圖示的名稱,以選取或 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 專案,並在應用程式內使用這些圖示。