Skip to content

Use SharedPreference (Android) and UserDefaults (iOS) with React Native over a unified interface

Notifications You must be signed in to change notification settings

joshuapinter/react-native-default-preference

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-default-preference

Use SharedPreference (Android) and UserDefaults (iOS) with React Native over a unified interface. All data are stored as String, in case more complex data structure is needed, serialize it (e.g. JSON)

Getting started

$ npm install react-native-default-preference --save

Mostly automatic installation

$ react-native link react-native-default-preference

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-default-preference and add RNDefaultPreference.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNDefaultPreference.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import com.reactlibrary.RNDefaultPreferencePackage; to the imports at the top of the file
  • Add new RNDefaultPreferencePackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-default-preference'
    project(':react-native-default-preference').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-default-preference/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-default-preference')
    

Usage

import DefaultPreference from 'react-native-default-preference';

DefaultPreference.get('my key').then(function(value) {console.log(value)});
DefaultPreference.set('my key', 'my value').then(function() {console.log('done')});

API

function get(key:String):Promise<String>;
function set(key:String, value:String):Promise<Void>;
function clear(key:String):Promise<Void>;
function getMultiple(keys:Array<String>):Promise<Object>;
function setMultiple(data:Object):Promise<Void>;
function clearMultiple(keys:Array<String>):Promise<Void>;
function getAll():Promise<Object>;
function clearAll():Promise<Void>;

About

Use SharedPreference (Android) and UserDefaults (iOS) with React Native over a unified interface

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 57.7%
  • Objective-C 40.5%
  • JavaScript 1.8%