├── .gitignore ├── .npmignore ├── CHANGELOG.md ├── LICENSE ├── RCTUserDefaults.h ├── RCTUserDefaults.m ├── RCTUserDefaults.podspec ├── RCTUserDefaults.xcodeproj └── project.pbxproj ├── README.md ├── UserDefaults.js ├── circle.yml └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | project.xcworkspace 24 | 25 | # node.js 26 | # 27 | node_modules/ 28 | *.log 29 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | project.xcworkspace 24 | 25 | # node.js 26 | # 27 | node_modules/ 28 | *.log 29 | CHANGELOG.md -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## Change Log 2 | 3 | ### upcoming (2016/11/08 13:53 +00:00) 4 | - [#8](https://github.com/wwayne/react-native-user-defaults/pull/8) add .podspec file for cocoapods support (@kyaroru) 5 | 6 | ### 0.1.4 (2016/05/24 09:53 +00:00) 7 | - [#5](https://github.com/wwayne/react-native-user-defaults/pull/5) fix JSON.parse error (shared NSUserDefaults) (@MacKentoch) 8 | - [#3](https://github.com/wwayne/react-native-user-defaults/pull/3) Update Doc and Release config (@kemcake) 9 | - [#2](https://github.com/wwayne/react-native-user-defaults/pull/2) Support NSUserDefaults with suiteName (@kemcake) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Wang Zixiao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /RCTUserDefaults.h: -------------------------------------------------------------------------------- 1 | #import "RCTBridge.h" 2 | 3 | @interface RCTUserDefaults : NSObject 4 | 5 | @end 6 | -------------------------------------------------------------------------------- /RCTUserDefaults.m: -------------------------------------------------------------------------------- 1 | #import "RCTUserDefaults.h" 2 | 3 | @implementation RCTUserDefaults 4 | 5 | -(NSUserDefaults*)userDefaultsForSuiteName:(NSString *)suiteName { 6 | if (suiteName && ![suiteName isEqualToString:@""]) { 7 | return [[NSUserDefaults alloc] initWithSuiteName:suiteName]; 8 | } 9 | return [NSUserDefaults standardUserDefaults]; 10 | } 11 | 12 | RCT_EXPORT_MODULE() 13 | 14 | RCT_EXPORT_METHOD(setObject:(NSString *)key value:(NSString *)value suiteName:(NSString *)suiteName callback:(RCTResponseSenderBlock)cb) { 15 | NSUserDefaults *userDefaults = [self userDefaultsForSuiteName:suiteName]; 16 | [userDefaults setObject:value forKey:key]; 17 | cb(@[[NSNull null] ,@"Save success"]); 18 | } 19 | 20 | RCT_EXPORT_METHOD(getObject:(NSString *)key suiteName:(NSString *)suiteName callback:(RCTResponseSenderBlock)cb) { 21 | NSUserDefaults *userDefaults = [self userDefaultsForSuiteName:suiteName]; 22 | NSString *result = [userDefaults stringForKey:key]; 23 | if (result) { 24 | cb(@[[NSNull null], result]); 25 | } else { 26 | cb(@[@YES]); 27 | } 28 | } 29 | 30 | RCT_EXPORT_METHOD(removeObject:(NSString *)key suiteName:(NSString *)suiteName callback:(RCTResponseSenderBlock)cb) { 31 | NSUserDefaults *userDefaults = [self userDefaultsForSuiteName:suiteName]; 32 | [userDefaults removeObjectForKey:key]; 33 | cb(@[[NSNull null] ,@"Remove success"]); 34 | } 35 | 36 | RCT_EXPORT_METHOD(empty:(NSString *)suiteName callback:(RCTResponseSenderBlock)cb) { 37 | 38 | NSArray *defaultSetting = @[@"RCTDevMenu", @"ApplePasscodeKeyboards", @"AddingEmojiKeybordHandled", 39 | @"MSVLoggingMasterSwitchEnabledKey", @"AppleITunesStoreItemKinds", 40 | @"NSInterfaceStyle", @"AppleLanguagesDidMigrate", @"AppleLanguages", 41 | @"NSLanguages", @"AppleKeyboardsExpanded", @"AppleKeyboards", @"AppleLocale"]; 42 | 43 | NSUserDefaults *userDefaults = [self userDefaultsForSuiteName:suiteName]; 44 | NSDictionary *defaultsDict = [userDefaults dictionaryRepresentation]; 45 | for (NSString *key in [defaultsDict allKeys]) { 46 | NSInteger index = [defaultSetting indexOfObject:key]; 47 | if (index == NSNotFound) { 48 | [userDefaults removeObjectForKey:key]; 49 | } 50 | } 51 | 52 | cb(@[[NSNull null] ,@"Empty success"]); 53 | } 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /RCTUserDefaults.podspec: -------------------------------------------------------------------------------- 1 | require 'json' 2 | version = JSON.parse(File.read('package.json'))["version"] 3 | 4 | Pod::Spec.new do |s| 5 | 6 | s.name = "RCTUserDefaults" 7 | s.version = version 8 | s.summary = "React Native User Defaults" 9 | s.homepage = "https://github.com/wwayne/react-native-user-defaults" 10 | s.license = "MIT" 11 | s.author = "wwayne" 12 | s.platform = :ios, "7.0" 13 | s.source = { :git => "https://github.com/wwayne/react-native-user-defaults", :tag => "v#{s.version}" } 14 | s.source_files = '*.{h,m}' 15 | s.preserve_paths = "**/*.js" 16 | s.dependency 'React' 17 | 18 | end 19 | -------------------------------------------------------------------------------- /RCTUserDefaults.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 13BE3DEE1AC21097009241FE /* RCTUserDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = 13BE3DED1AC21097009241FE /* RCTUserDefaults.m */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXCopyFilesBuildPhase section */ 14 | 58B511D91A9E6C8500147676 /* CopyFiles */ = { 15 | isa = PBXCopyFilesBuildPhase; 16 | buildActionMask = 2147483647; 17 | dstPath = "include/$(PRODUCT_NAME)"; 18 | dstSubfolderSpec = 16; 19 | files = ( 20 | ); 21 | runOnlyForDeploymentPostprocessing = 0; 22 | }; 23 | /* End PBXCopyFilesBuildPhase section */ 24 | 25 | /* Begin PBXFileReference section */ 26 | 134814201AA4EA6300B7C361 /* libRCTUserDefaults.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTUserDefaults.a; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | 13BE3DEC1AC21097009241FE /* RCTUserDefaults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTUserDefaults.h; sourceTree = ""; }; 28 | 13BE3DED1AC21097009241FE /* RCTUserDefaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTUserDefaults.m; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | 58B511D81A9E6C8500147676 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | 134814211AA4EA7D00B7C361 /* Products */ = { 43 | isa = PBXGroup; 44 | children = ( 45 | 134814201AA4EA6300B7C361 /* libRCTUserDefaults.a */, 46 | ); 47 | name = Products; 48 | sourceTree = ""; 49 | }; 50 | 58B511D21A9E6C8500147676 = { 51 | isa = PBXGroup; 52 | children = ( 53 | 13BE3DEC1AC21097009241FE /* RCTUserDefaults.h */, 54 | 13BE3DED1AC21097009241FE /* RCTUserDefaults.m */, 55 | 134814211AA4EA7D00B7C361 /* Products */, 56 | ); 57 | sourceTree = ""; 58 | }; 59 | /* End PBXGroup section */ 60 | 61 | /* Begin PBXNativeTarget section */ 62 | 58B511DA1A9E6C8500147676 /* RCTUserDefaults */ = { 63 | isa = PBXNativeTarget; 64 | buildConfigurationList = 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RCTUserDefaults" */; 65 | buildPhases = ( 66 | 58B511D71A9E6C8500147676 /* Sources */, 67 | 58B511D81A9E6C8500147676 /* Frameworks */, 68 | 58B511D91A9E6C8500147676 /* CopyFiles */, 69 | ); 70 | buildRules = ( 71 | ); 72 | dependencies = ( 73 | ); 74 | name = RCTUserDefaults; 75 | productName = RCTDataManager; 76 | productReference = 134814201AA4EA6300B7C361 /* libRCTUserDefaults.a */; 77 | productType = "com.apple.product-type.library.static"; 78 | }; 79 | /* End PBXNativeTarget section */ 80 | 81 | /* Begin PBXProject section */ 82 | 58B511D31A9E6C8500147676 /* Project object */ = { 83 | isa = PBXProject; 84 | attributes = { 85 | LastUpgradeCheck = 0610; 86 | ORGANIZATIONNAME = Facebook; 87 | TargetAttributes = { 88 | 58B511DA1A9E6C8500147676 = { 89 | CreatedOnToolsVersion = 6.1.1; 90 | }; 91 | }; 92 | }; 93 | buildConfigurationList = 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RCTUserDefaults" */; 94 | compatibilityVersion = "Xcode 3.2"; 95 | developmentRegion = English; 96 | hasScannedForEncodings = 0; 97 | knownRegions = ( 98 | en, 99 | ); 100 | mainGroup = 58B511D21A9E6C8500147676; 101 | productRefGroup = 58B511D21A9E6C8500147676; 102 | projectDirPath = ""; 103 | projectRoot = ""; 104 | targets = ( 105 | 58B511DA1A9E6C8500147676 /* RCTUserDefaults */, 106 | ); 107 | }; 108 | /* End PBXProject section */ 109 | 110 | /* Begin PBXSourcesBuildPhase section */ 111 | 58B511D71A9E6C8500147676 /* Sources */ = { 112 | isa = PBXSourcesBuildPhase; 113 | buildActionMask = 2147483647; 114 | files = ( 115 | 13BE3DEE1AC21097009241FE /* RCTUserDefaults.m in Sources */, 116 | ); 117 | runOnlyForDeploymentPostprocessing = 0; 118 | }; 119 | /* End PBXSourcesBuildPhase section */ 120 | 121 | /* Begin XCBuildConfiguration section */ 122 | 58B511ED1A9E6C8500147676 /* Debug */ = { 123 | isa = XCBuildConfiguration; 124 | buildSettings = { 125 | ALWAYS_SEARCH_USER_PATHS = NO; 126 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 127 | CLANG_CXX_LIBRARY = "libc++"; 128 | CLANG_ENABLE_MODULES = YES; 129 | CLANG_ENABLE_OBJC_ARC = YES; 130 | CLANG_WARN_BOOL_CONVERSION = YES; 131 | CLANG_WARN_CONSTANT_CONVERSION = YES; 132 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 133 | CLANG_WARN_EMPTY_BODY = YES; 134 | CLANG_WARN_ENUM_CONVERSION = YES; 135 | CLANG_WARN_INT_CONVERSION = YES; 136 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 137 | CLANG_WARN_UNREACHABLE_CODE = YES; 138 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 139 | COPY_PHASE_STRIP = NO; 140 | ENABLE_STRICT_OBJC_MSGSEND = YES; 141 | GCC_C_LANGUAGE_STANDARD = gnu99; 142 | GCC_DYNAMIC_NO_PIC = NO; 143 | GCC_OPTIMIZATION_LEVEL = 0; 144 | GCC_PREPROCESSOR_DEFINITIONS = ( 145 | "DEBUG=1", 146 | "$(inherited)", 147 | ); 148 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 149 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 150 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 151 | GCC_WARN_UNDECLARED_SELECTOR = YES; 152 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 153 | GCC_WARN_UNUSED_FUNCTION = YES; 154 | GCC_WARN_UNUSED_VARIABLE = YES; 155 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 156 | MTL_ENABLE_DEBUG_INFO = YES; 157 | ONLY_ACTIVE_ARCH = YES; 158 | SDKROOT = iphoneos; 159 | }; 160 | name = Debug; 161 | }; 162 | 58B511EE1A9E6C8500147676 /* Release */ = { 163 | isa = XCBuildConfiguration; 164 | buildSettings = { 165 | ALWAYS_SEARCH_USER_PATHS = NO; 166 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 167 | CLANG_CXX_LIBRARY = "libc++"; 168 | CLANG_ENABLE_MODULES = YES; 169 | CLANG_ENABLE_OBJC_ARC = YES; 170 | CLANG_WARN_BOOL_CONVERSION = YES; 171 | CLANG_WARN_CONSTANT_CONVERSION = YES; 172 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 173 | CLANG_WARN_EMPTY_BODY = YES; 174 | CLANG_WARN_ENUM_CONVERSION = YES; 175 | CLANG_WARN_INT_CONVERSION = YES; 176 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 177 | CLANG_WARN_UNREACHABLE_CODE = YES; 178 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 179 | COPY_PHASE_STRIP = YES; 180 | ENABLE_NS_ASSERTIONS = NO; 181 | ENABLE_STRICT_OBJC_MSGSEND = YES; 182 | GCC_C_LANGUAGE_STANDARD = gnu99; 183 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 184 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 185 | GCC_WARN_UNDECLARED_SELECTOR = YES; 186 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 187 | GCC_WARN_UNUSED_FUNCTION = YES; 188 | GCC_WARN_UNUSED_VARIABLE = YES; 189 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 190 | MTL_ENABLE_DEBUG_INFO = NO; 191 | SDKROOT = iphoneos; 192 | VALIDATE_PRODUCT = YES; 193 | }; 194 | name = Release; 195 | }; 196 | 58B511F01A9E6C8500147676 /* Debug */ = { 197 | isa = XCBuildConfiguration; 198 | buildSettings = { 199 | HEADER_SEARCH_PATHS = ( 200 | "$(inherited)", 201 | /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, 202 | "$(SRCROOT)/../../React/**", 203 | "$(SRCROOT)/../../node_modules/react-native/React/**", 204 | ); 205 | LIBRARY_SEARCH_PATHS = "$(inherited)"; 206 | OTHER_LDFLAGS = "-ObjC"; 207 | PRODUCT_NAME = RCTUserDefaults; 208 | SKIP_INSTALL = YES; 209 | }; 210 | name = Debug; 211 | }; 212 | 58B511F11A9E6C8500147676 /* Release */ = { 213 | isa = XCBuildConfiguration; 214 | buildSettings = { 215 | HEADER_SEARCH_PATHS = ( 216 | "$(inherited)", 217 | /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, 218 | "$(SRCROOT)/../../React/**", 219 | "$(SRCROOT)/../../node_modules/react-native/React/**", 220 | ); 221 | LIBRARY_SEARCH_PATHS = "$(inherited)"; 222 | OTHER_LDFLAGS = "-ObjC"; 223 | PRODUCT_NAME = RCTUserDefaults; 224 | SKIP_INSTALL = YES; 225 | }; 226 | name = Release; 227 | }; 228 | /* End XCBuildConfiguration section */ 229 | 230 | /* Begin XCConfigurationList section */ 231 | 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RCTUserDefaults" */ = { 232 | isa = XCConfigurationList; 233 | buildConfigurations = ( 234 | 58B511ED1A9E6C8500147676 /* Debug */, 235 | 58B511EE1A9E6C8500147676 /* Release */, 236 | ); 237 | defaultConfigurationIsVisible = 0; 238 | defaultConfigurationName = Release; 239 | }; 240 | 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RCTUserDefaults" */ = { 241 | isa = XCConfigurationList; 242 | buildConfigurations = ( 243 | 58B511F01A9E6C8500147676 /* Debug */, 244 | 58B511F11A9E6C8500147676 /* Release */, 245 | ); 246 | defaultConfigurationIsVisible = 0; 247 | defaultConfigurationName = Release; 248 | }; 249 | /* End XCConfigurationList section */ 250 | }; 251 | rootObject = 58B511D31A9E6C8500147676 /* Project object */; 252 | } 253 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-user-defaults 2 | [![Version](http://img.shields.io/npm/v/react-native-user-defaults.svg)](https://www.npmjs.org/package/react-native-user-defaults) 3 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) 4 | [![Circle CI](https://circleci.com/gh/wwayne/react-native-user-defaults/tree/master.svg?style=svg)](https://circleci.com/gh/wwayne/react-native-user-defaults/tree/master) 5 | 6 | ## When to use UserDefaults 7 | when you want to store some small ,insensitive and permanent information in your app 8 | 9 | ## Installation 10 | 11 | 1. `npm install react-native-user-defaults` 12 | 2. open xcode, right click on `Libraries`, then click `Add Files...`, select `node_modules -> react-native-user-defaults -> RCTUserDefaults.xcodeproj` 13 | 3. still in xcode, select main project file, then `Build Phases -> Link Binary... -> Add items -> libRCTUserDefaults.a` 14 | 15 | If you are not clear about the step 2 and 3, you can check react-native [official doc](https://facebook.github.io/react-native/docs/linking-libraries-ios.html#content) , follow the step 1 and 2 in the official doc. 16 | 17 | ## Usage 18 | In **objective-c** and **swift**, you have to use specific method for specific type, like `setObject` and `stringForKey`. But in **react-native**, I believe that all you need is just `set` and `get`. 19 | 20 | Every method supports `callback` and `promise` 21 | 22 | ``` 23 | import userDefaults from 'react-native-user-defaults' 24 | ``` 25 | #### Set information for a key 26 | 27 | ``` 28 | set({String}, {String, Number, Bool, Object, Array}, [,suiteName] [,callback]) 29 | 30 | Example: 31 | userDefaults.set("key1", "valueIsString") 32 | .then(data => console.log(data)) // Save success 33 | 34 | userDefaults.set("key2", [1, true], "group.com.company.app", (err, data) => { 35 | if(!err) console.log(data) // Save success 36 | }) 37 | ``` 38 | 39 | #### Get information of a key 40 | 41 | ``` 42 | get({String} [,suiteName] [,callback]) 43 | 44 | Example: 45 | userDefaults.get("key1") 46 | .then(data => console.log(data)) // value for the key1 47 | 48 | userDefaults.get("key2", "group.com.company.app", (err, data) => { 49 | if(!err) console.log(data) // value for the key2 50 | }) 51 | ``` 52 | 53 | #### Remove an item 54 | 55 | ``` 56 | remove({String} [,suiteName] [,callback]) 57 | 58 | Example: 59 | userDefaults.remove("key1") 60 | .then(data => console.log(data)) // Remove success 61 | ``` 62 | 63 | #### Empty all items which are not default(APP default settings will be reserved) 64 | 65 | ``` 66 | empty([suiteName] [,callback]) 67 | 68 | Example: 69 | userDefaults.empty() 70 | .then(data => console.log(data)) // Empty success 71 | ``` 72 | 73 | ## Troubleshooting 74 | 75 | 1. No tests? I have tested all methods, I promise. I've checked other famous react-native components, they all don't have tests, I think we are all seeking a way of formal tests. 76 | 2. Why not swift? I wrote this in swift at the beginning, but I find it's hard for others and even myself to integrate it into an existed project. 77 | 3. Type bug? if you `set('key1', '12')`, then `get('key1')`, you will get `12`, not `'12'`, I don't take this as a bug so that we can use this component more conveniently. If you don't think so, tell me. 78 | 79 | ## License 80 | MIT 81 | -------------------------------------------------------------------------------- /UserDefaults.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | import React from 'react-native' 4 | const { 5 | NativeModules: { 6 | UserDefaults, 7 | }, 8 | } = React 9 | 10 | const userDefaults = { 11 | set: (key, value, suiteName, cb) => { 12 | const jsonValue = typeof value === 'string' || typeof value === 'number' ? value : JSON.stringify(value) 13 | return new Promise((resolve, reject) => { 14 | UserDefaults.setObject(key, jsonValue, suiteName, (err, data) => { 15 | if (err) { 16 | const error = new Error(`Set fail for key: ${key}`) 17 | cb && cb(error) 18 | reject(error) 19 | } else { 20 | cb && cb(null, data) 21 | resolve(data) 22 | } 23 | }) 24 | }); 25 | }, 26 | 27 | get: (key, suiteName, cb) => { 28 | return new Promise((resolve, reject) => { 29 | UserDefaults.getObject(key, suiteName, (err, data) => { 30 | if (err) { 31 | const error = new Error(`Get fail for key: ${key}`) 32 | cb && cb(error) 33 | reject(error) 34 | } else { 35 | const result = typeof data === 'string' || typeof data === 'number' ? data : JSON.parse(data) 36 | cb && cb(null, result) 37 | resolve(result) 38 | } 39 | }) 40 | }) 41 | }, 42 | 43 | remove: (key, suiteName, cb) => { 44 | return new Promise((resolve, reject) => { 45 | UserDefaults.removeObject(key, suiteName, (err, data) => { 46 | if (err) { 47 | const error = new Error(`Remove fail for key: ${key}`) 48 | cb && cb(error) 49 | reject(error) 50 | } else { 51 | cb && cb(null, data) 52 | resolve(data) 53 | } 54 | }) 55 | }) 56 | }, 57 | 58 | empty: (suiteName, cb) => { 59 | return new Promise((resolve, reject) => { 60 | UserDefaults.empty(suiteName, (err, data) => { 61 | if (err) { 62 | const error = new Error('Empty fail') 63 | cb && cb(error) 64 | reject(error) 65 | } else { 66 | cb && cb(null, data) 67 | resolve(data) 68 | } 69 | }) 70 | }) 71 | } 72 | } 73 | 74 | export default userDefaults 75 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | machine: 2 | node: 3 | version: 5.1.1 4 | 5 | dependencies: 6 | override: 7 | - npm install -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-user-defaults", 3 | "version": "0.1.5", 4 | "main": "UserDefaults.js", 5 | "description": "ios UserDefaults used by react-native", 6 | "author": "wwayne", 7 | "scripts": { 8 | "test": "standard ./lib/userDefaults.js", 9 | "build": "./node_modules/.bin/babel lib/userDefaults.js > ./UserDefaults.js" 10 | }, 11 | "homepage": "https://github.com/wwayne/react-native-user-defaults#readme", 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/wwayne/react-native-user-defaults.git" 15 | }, 16 | "bugs": { 17 | "url": "https://github.com/wwayne/react-native-user-defaults/issues" 18 | }, 19 | "peerDependencies": {}, 20 | "dependencies": {}, 21 | "devDependencies": { 22 | "babel": "^5.8.34", 23 | "standard": "^5.4.1" 24 | }, 25 | "keywords": [ 26 | "ios", 27 | "react-native", 28 | "object-c", 29 | "userdefaults" 30 | ], 31 | "license": "MIT" 32 | } 33 | --------------------------------------------------------------------------------