Storing oauth token in the filesystem

My app needs to send an Oauth access token with my REST requests. To do this I implement Retrofit Interceptor in my common module, along with my services.

It means that I must be able to store the token to retrieve it in the Interceptor each time I call the API. But to save in the filesystem - which I thought was the simplest way - I need the base directory, which I can obtain only from the native code.

So basically what I would need to do is passing the base directory through the following classes:
native view -> presenter -> service creator -> interceptor
in each presenter of my app that needs to request the API.

How could I share this base directory in my whole app, once and for all? Or could I store my oauth token in a UI-independent way? I also looked at an SQLite implementation but it seems overkill.


1 Like

In your common project, you should define an interface:

public interface IPlatform {
    public String getBaseDirectory();

And a singleton, where you can access the current platform:

public class PlatformService {

    public static IPlatform getPlatform() { /* .... */ }

    public static void initPlatform(IPlatform p) { /* ... */ }

In your main entry point (e.g. Android Application class, main() method on iOS), you instantiate the platform specific implementation of IPlatform and call PlatformService.initPlatform() with it.

Now you can access platform specific services / variables in a platform independent manner.

Best Regards,

1 Like

Sounds good, thanks for the trick :wink:

I wrap SharedPreferences and NSUserdefaults via an interface:

public interface DataStore {
 * Get an integer value
 * @param key   The value's key
 * @return  the value, or 0 if key not found
int getInt(String key);

float getFloat(String key);

String getString(String key);

long getLong(String key);

void putInt(String key, int value);

void putFloat(String key, float value);

void putString(String key, String value);

void putLong(String s, long value);

The Android implementation:

public class AndroidDataStore implements DataStore {

private SharedPreferences prefs;

public AndroidDataStore(SharedPreferences defaultSharedPreferences) {
    this.prefs = defaultSharedPreferences;

public int getInt(String key) {
    return prefs.getInt(key, 0);

public float getFloat(String key) {
    return prefs.getFloat(key, 0);
// etc.....

For iOS (this is RoboVm code, MOE should be similar)

public class IosDataStore implements DataStore {

private NSUserDefaults userDefaults;

public IosDataStore(NSUserDefaults userDefaults) {
    this.userDefaults = userDefaults;

public int getInt(String key) {
    return userDefaults.getInt(key);

public float getFloat(String key) {
    return userDefaults.getFloat(key);
// etc.....