Coder Social home page Coder Social logo

kgibbe1 / react-native-default-preference Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kevinresol/react-native-default-preference

0.0 0.0 0.0 102 KB

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

License: MIT License

Java 49.51% JavaScript 1.45% Objective-C 41.19% Ruby 7.85%

react-native-default-preference's Introduction

npm version

react-native-default-preference

Use SharedPreferences (Android) and UserDefaults (iOS) with React Native over a unified interface. All data is stored as a string. If you need to support more complex data structures, use serialization/deserialization (e.g. JSON).

Getting started

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

React Native >= 0.60

$ cd ios && pod install

React Native <= 0.59

$ 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/[...]/MainApplication.java
  • Add import com.kevinresol.react_native_default_preference.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<Array<String>>;
function setMultiple(data:Object):Promise<Void>;
function clearMultiple(keys:Array<String>):Promise<Void>;
function getAll():Promise<Object>;
function clearAll():Promise<Void>;

/** Gets and sets the current preferences file name (android) or user default suite name (ios) **/
function getName():Promise<String>;
function setName(name:String):Promise<Void>;

Cordova Native Storage Compatibility

This module is compatible with cordova-plugin-native-storage.

Android

You need to use the same SharedPreference as in the cordova plugin, to do so add the following line:

import { Platform } from 'react-native';
// ...
if (Platform.OS === 'android') DefaultPreference.setName('NativeStorage');

iOS

You don't need to change the name, as cordova-plugin-native-storage uses the default value.

react-native-default-preference's People

Contributors

kevinresol avatar marcelkalveram avatar cristovao-trevisan avatar karanjthakkar avatar nekith avatar dozel avatar gabrieleprestifilippo avatar jesuskfc avatar kgibbe1 avatar mspanc avatar tagir-a avatar emnsen avatar knarfytrebil avatar juliano-abr avatar ybrsangwoo avatar vennos5 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.