Giter VIP home page Giter VIP logo

react-native-userdefaults-ios's Introduction

react-native-userdefaults-ios Build Status

React Native Module for NSUserDefaults

This library is especially helpful for hybrid apps that already make use of [NSUserDefaults standardUserDefaults] and would like to read or write to it from within their React components.

NPM

Add it to your project

  1. Run npm install react-native-userdefaults-ios --save
  2. Open your project in XCode, right click on Libraries and click Add Files to "Your Project Name".
  3. Select the RNUserDefaults.xcodeproj file in the node_modules/react-native-userdefaults-ios folder and click Add
  4. In the Xcode Project Navigator find the RNUserDefaults.xcodproj and add the Products/libRNUserDefaultsIOS.a to Build Phases -> Link Binary With Libraries.
  5. Follow the implementation example below...

Example

// Require the library...
var UserDefaults = require('react-native-userdefaults-ios');

Writing to standardUserDefaults

//Set an Array...
var arr = ['1', '2', '3'];
UserDefaults.setArrayForKey(arr, 'keyForMyArray')
    .then(result => {
        console.log(result);
    });

// Set a String...
UserDefaults.setStringForKey('myString', 'keyForMyString')
    .then(result => {
        console.log(result);
    });

//Set an Object...
var obj = {
    name: 'Dave'
};
UserDefaults.setObjectForKey(obj, 'keyForMyObject')
    .then(result => {
        console.log(result);
    });

//Set a boolean value...
UserDefaults.setBoolForKey(true, 'keyForMyBool')
    .then(result => {
        console.log(result);
    });

//Remove an item (works for any type)...
UserDefaults.removeItemForKey('keyOfItemToRemove')
    .then(result => {
        console.log(result);
    });

Reading from standardUserDefaults

// Get an array for a given key...
UserDefaults.arrayForKey('keyForMyArray')
    .then(array => {
        //Do something with the returned array...
        array.forEach(item => {
            console.log(item);
        });
    });

// Get a string for a given key...
UserDefaults.stringForKey('keyForMyString')
    .then(string => {
        //Do something with the returned string...
        console.log(string);
    });

// Get an object for a given key...
UserDefaults.objectForKey('keyForMyObject')
    .then(obj => {
        //Do something with the returned object...
        console.log(obj);
    });

// Get a boolean value for a given key...
UserDefaults.boolForKey('keyForMyBool')
    .then(bool => {
        //Do something with the returned boolean value...
        console.log(bool);
    });

Todos for 1.0 release

  • Implement dataForKey:
  • Implement stringArrayForKey:
  • Implement setFloat:forKey:
  • Implement floatForKey:
  • Implement setInteger:forKey:
  • Implement integerForKey:
  • Implement setDouble:forKey:
  • Implement doubleForKey:
  • Implement setURL:forKey:
  • Implement URLForKey:
  • Implement NSUserDefaultsDidChangeNotification

Todos for 0.1.0 release - DONE!

  • Implement arrayForKey:
  • Implement stringForKey:
  • Implement setObject:forKey:
  • Implement objectForKey:
  • Implement removeObjectForKey:
  • Implement dictionaryForKey: Note: This was taken care of with objectForKey since in JS an Object is a Dictionary in Obj-C
  • Implement setBool:forKey:
  • Implement boolForKey:

react-native-userdefaults-ios's People

Contributors

dsibiski avatar ccheever avatar jsierles avatar

Watchers

Slavik 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.