2

I need to share some particular data among two separate apps on android, preferably make some kind of shared folder which can be accessed by both the apps only.

Why? I need a single sign on functionality between two separate apps.

Till now I have been authenticating users using JWT tokens by storing them in sharedpreferences (storing them to maintain the signin) and clearing the shared prefs when the user logs out (along with hitting the api which changes the token).

Now I have another app which works with the same token.

What's required: you sign into one and another one automatically signs in. So, my plan is to store the token on the device which in turn can be accessed by both the apps.

I know I will need an active directory for a secure SSO but I am okay with not having a 100% security as of now, the question still remains about how to share data among apps.

2
  • I would suggest, in the signin, have a key in the backend user data as isSignedIn, update it, during signing in and signing out. With that, you can check for the key while opening up the app, whether the user is signed in other devices or not. Let me know if that helps or you don't get the idea Commented May 26, 2020 at 6:53
  • Please check medium.com/@ranaranvijaysingh9/… Shared services normally has been used to share data between two apps Commented May 26, 2020 at 7:06

1 Answer 1

2

I'm just giving you an idea, and not to mention this ONLY works in Android.

  1. From your 1st app (primary), create a file (for now, I am creating a .txt file) in device external storage using path_provider,

    Future<void> _createAndWrite() async {
      // Get external directory
      final directory = (await Directory('storage/emulated/0/Sharing Folder').create()).path;
    
      // Create your file
      final file = File('$directory/any_file.txt');
      await file.create();
    
      // Write to the file
      await file.writeAsString('Content to write');
    }
    
  2. In your 2nd app, you can read the contents of the file with something like:

    Future<void> _readFile() async {
      // Get external directory
      final directory = (await Directory('storage/emulated/0/Sharing Folder').create()).path;
    
      // Get the file
      final file = File('$directory/any_file.txt');
    
      // Read it
      final content = await file.readAsString();
    }
    

Make sure in both apps you have declared the following permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Sign up to request clarification or add additional context in comments.

2 Comments

But other apps on device will also be able to access this .txt right?
Yes, you're right. But you mentioned in your post that for now you're not concerned with security part, or you may want to create a different type of file which can allow encryption/decryption.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.