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 .