2

I'm trying to get a string (UserID) using Preferences.Get (Xamarin.Essentials) on a PushNotification.Extension project, but as the Preferences.Set happens in the Xamarin iOS project, I'm always getting an empty string in the extensions project.

Is there a way to share this preference between the iOS project and the iOS.extension?

public string UserID
{
    get
    {
        return Preferences.Get(nameof(UserID), UserIDDefault);
    }
    set
    {
        Preferences.Set(nameof(UserID), value);
    }
}
dkackman
  • 15,179
  • 13
  • 69
  • 123
idenardi
  • 600
  • 1
  • 6
  • 20

2 Answers2

1

According to Apple docs , please follow the steps to enable data-sharinig .

  1. Enable App Groups Capabilities , refer to App Group Capabilities in Xamarin.iOS.

  2. Add the app to the App Group .

  3. Use NSUserDefaults and init it with the name of the extension bundle identifier.

//Save 
var defaults = new NSUserDefaults(@"com.example.domain.MyShareExtension");
defaults.SetString("value","Mykey");
defaults.Synchronize();
//Get
var defaults = new NSUserDefaults(@"com.example.domain.MyShareExtension");
var value = defaults.ValueForKey("Mykey");
ColeX
  • 14,062
  • 5
  • 43
  • 240
0

"NSUserDefaults(string)" does not work for me. In the Xamarin iOS documentation this constructor is marked as deprecated! see https://learn.microsoft.com/en-us/dotnet/api/foundation.nsuserdefaults?view=xamarin-ios-sdk-12

But when I use "NSUserDefaults(String, NSUserDefaultsType)" it runs perfect.