titaniumtitanium-mobiletitanium-modulestitanium-android

Access `Titanium.App.Properties` in native app


TLDR: Is there is any native way to access the Titanium.App.Properties for both Android and iOS?

I am working on a project where the original app was made with Titanium, and the new updated version is built with Xamarin.

The original developers thought it would be smart to save a Device Token in the Titanium.App.Properties that would be used to access all the users history and purchases.

The device token is not stored anywhere else, so the only way to get to it is through Titanium.App.Properties.

I have the key they used to store the token. So my question is, is there is any native way to access the Titanium.App.Properties for both Android and iOS?


From this doc there is a paragraph: "Both iOS and Android store app properties in special files on the filesystem. Natively, iOS properties are known as NSUserDefaults, which are stored in .plist files in the application's library directory. Android stores them in standard xml text files at /data/data/com.domainname.appname/shared_prefs/titanium.xml. Titanium provides a uniform means to set and get app properties via the Titanium.App.Properties API."

From this SO question it sounds like a module needs to be used: "Other iOS applications cannot access these properties and native Android modules must use the Titanium module API TiApplication.getAppProperties method to access these properties."


Solution

  • For Android the Titanium properties are stored in the shared preferences with the path titanium.

    ISharedPreferences TitaniumSharedPreferences = CrossCurrentActivity.Current.Activity.GetSharedPreferences("titanium", Android.Content.FileCreationMode.Private);

    Then simply use the shared preference to get any property you need.

    var userToken = TitaniumSharedPreferences.GetString("UserToken", string.Empty);


    For iOS it is simpler, it just uses the NSUserDefaults.

    var userToken = NSUserDefaults.StandardUserDefaults.StringForKey("UserToken");