Pages

Sunday, 10 January 2021

XamariniOS-ChangeAppIcon Programmatically

 

Using Dependency Service we can Do this so first we start with interface creation in our portable projects.

   public interface IAppIconSwitchService


    {

        Task SwitchAppIcon(string iconName);


    }



and in MainPage.xaml

async void Xamarin_Button_Clicked(System.Object sender, System.EventArgs e)

{

var iconSwitcher = DependencyService.Get<IAppIconSwitchService>();
await iconSwitcher.SwitchAppIcon(null);

}



async void Maui_Button_Clicked(System.Object sender, System.EventArgs e)

{
var iconSwitcher = DependencyService.Get<IAppIconSwitchService>();
await iconSwitcher.SwitchAppIcon("MauiLogo");


}

using System;


using System.Threading.Tasks;


using AppIconUpdater;


using AppIconUpdater.iOS;


using UIKit;


using Xamarin.Forms;


[assembly: Xamarin.Forms.Dependency(typeof(AppIconSwitchService))]


namespace AppIconUpdater.iOS


{


    public class AppIconSwitchService : IAppIconSwitchService


    {


        public async Task SwitchAppIcon(string iconName)


        {


            if (UIApplication.SharedApplication.SupportsAlternateIcons)


            {


                if (iconName == "MauiLogo")


                    await UIApplication.SharedApplication.SetAlternateIconNameAsync(iconName);


                else


                    UIApplication.SharedApplication.SetAlternateIconName(null, (error) => { Console.WriteLine(error); });


            }


        }


    }


}

Keep your logo Under Resources/ in IOS project and Create Resource Dictionary in info.plist
as we used await iconSwitcher.SwitchAppIcon("MauiLogo"); so it will get the logo.




1 comment:

  1. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site. For more info:- Xamarin App Development

    ReplyDelete