0

my problem is the following: I would like to update the Preferences (Xamarin.Essentials) when global settings of my application in iOS are changed at runtime.

I know that I have to use NSUserDefaults in order to access the global settings or respectively what is written in root.plist-file. I've also read the following: Be notified of change in a setting in Settings Bundle (in Xamarin iOS). But I don't know how to handle that. In the iOS part of my Xamarin project I have the AppDelegate-class but I don't know which method to call in order to change the preferences by NSUserDefaults. Could you help me?

Thanks in Advance.

1 Answers1

1

How to update Preferences when global settings in iOS are changed at runtime in Xamarin

If you want to update the Preferences (Xamarin.Essentials) from native ios platform, a simple method is to use Xamarin.Forms MessagingCenter.You can send message by method MessagingCenter.Send in xamarin ios and you can receive message in method MessagingCenter.Subscribe in forms app, here you can update the Preferences (Xamarin.Essentials).

The Xamarin.Forms MessagingCenter class implements the publish-subscribe pattern, allowing message-based communication between components that are inconvenient to link by object and type references. This mechanism allows publishers and subscribers to communicate without having a reference to each other, helping to reduce dependencies between them.

Publishers send messages using the MessagingCenter.Send method, while subscribers listen for messages using the MessagingCenter.Subscribe method. In addition, subscribers can also unsubscribe from message subscriptions, if required, with the MessagingCenter.Unsubscribe method.

Publish a message

MessagingCenter messages are strings. Publishers notify subscribers of a message with one of the MessagingCenter.Send overloads.

MessagingCenter.Send<MainPage, string>(this, "Hi", "John");

Subscribe to a message

Subscribers can register to receive a message using one of the MessagingCenter.Subscribe overloads. The following code example shows an example of this:

MessagingCenter.Subscribe<MainPage, string>(this, "Hi", async (sender, arg) =>
{
    await DisplayAlert("Message received", "arg=" + arg, "OK");
});

For more details, you can check:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center .

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19