gadildafissh
gadildafissh

Reputation: 2402

MAUI iOS saving Preferences in Debug build, but not in release build deployed to app store

When I build my app in VS 2022 on Mac or Windows in Debug configuration, preferences save and read fine. However, once I build the app in Release configuration and deploy it to the app store to be tested on TestFlight the Preferences no longer save/read.

Note: When I build and publish an Android release version, the preferences save/read correctly.

Here are the save and read methods:

public void CreateItem(string key, dynamic value) {
    Preferences.Default.Set(key, value);
}

public string GetString(string key) {
    String contents = null;

    try {
        contents = Preferences.Get(key, null);
    }
    catch (Exception ex) {
    }
    return contents;
}

Update I built the app in DEBUG and deployed it to my phone in these 2 ways with different results:

  1. Using the app's "Developer Cert" and "Developer Provisioning Profile", which results in save/read for Preferences working.

  2. Using the app's "Distribution Cert" and "Distribution Provisioning Profile", which results in save/read for Preferences NOT working.

Upvotes: 2

Views: 576

Answers (1)

gadildafissh
gadildafissh

Reputation: 2402

So, I wasn't able to resolve the issue with saving to Preferences, nor was I able to get a clear error message since it was a Release build and can't be connected to VS for debugging where I could see my console print statements in my try/catch blocks. I suppose a workaround to that would be to try and print those messages in a alert like so: DisplayAlert("Alert", "<error message body here>", "OK");

I did however find a profitable solution which was to save my JSON objects and strings to files like so:

public class LocalStorageService {
    private string documentsFolder;

    public LocalStorageService() {
        this.documentsFolder = FileSystem.AppDataDirectory;
    }

    public string ReadTextFile(string fileName) {
        string data = null;

        try
        {
            string path = Path.Combine(documentsFolder, $"{fileName}.json");

            if (File.Exists(path))
            {
                data = File.ReadAllText(path);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error in LocalStorageService::ReadTextFile(): {ex.Message}");
    }

        return data;
    }


    public void WriteToFile(string fileName, string json) {
        try
        {
            string path = Path.Combine(documentsFolder, $"{fileName}.json");
            File.WriteAllText(path, json);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error in LocalStorageService::WriteToFile(): {ex.Message}");
        }
    }
}

I call the save and read like this:

//write
localStorageService.WriteToFile(LocalStorageItemNames.USER_PROFILE, JsonConvert.SerializeObject(profileModel)

//read
string model = localStorageService.ReadTextFile(LocalStorageItemNames.USER_METAS);
if (!string.IsNullOrEmpty(model)) {
    profileModel = JsonConvert.DeserializeObject<ProfileModel>(model);
}

Upvotes: 0

Related Questions