issue #1529:use NSUserDefault to implement CCUserDefault on iOS

This commit is contained in:
minggo 2013-03-05 14:53:37 +08:00
parent e10123af19
commit f37b1525b4
6 changed files with 193 additions and 4 deletions

View File

@ -50,7 +50,7 @@ THE SOFTWARE.
#include "CCAccelerometer.h"
#include "sprite_nodes/CCAnimationCache.h"
#include "touch_dispatcher/CCTouch.h"
#include "support/CCUserDefault.h"
#include "support/CCUserDefault/CCUserDefault.h"
#include "shaders/ccGLStateCache.h"
#include "shaders/CCShaderCache.h"
#include "kazmath/kazmath.h"

View File

@ -221,7 +221,7 @@ THE SOFTWARE.
#include "support/CCNotificationCenter.h"
#include "support/CCPointExtension.h"
#include "support/CCProfiling.h"
#include "support/CCUserDefault.h"
#include "support/CCUserDefault/CCUserDefault.h"
#include "support/CCVertex.h"
#include "support/tinyxml2/tinyxml2.h"

View File

@ -1 +1 @@
502d05073a521dc7f534fb881b225d1ad5c8fde6
6cfbb92a36ed9c81afdc3a72359e730ee588ba32

View File

@ -24,7 +24,9 @@ THE SOFTWARE.
#include "CCUserDefault.h"
#include "platform/CCCommon.h"
#include "platform/CCFileUtils.h"
#include "tinyxml2/tinyxml2.h"
#include "../tinyxml2/tinyxml2.h"
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)
// root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
@ -441,3 +443,5 @@ void CCUserDefault::flush()
}
NS_CC_END
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)

View File

@ -0,0 +1,185 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#import "CCUserDefault.h"
#import <string>
#import <UIKit/UIKit.h>
#import "platform/CCPlatformConfig.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
NS_CC_BEGIN
using namespace std;
/**
* implements of CCUserDefault
*/
CCUserDefault* CCUserDefault::m_spUserDefault = 0;
string CCUserDefault::m_sFilePath = string("");
bool CCUserDefault::m_sbIsFilePathInitialized = false;
/**
* If the user invoke delete CCUserDefault::sharedUserDefault(), should set m_spUserDefault
* to null to avoid error when he invoke CCUserDefault::sharedUserDefault() later.
*/
CCUserDefault::~CCUserDefault()
{
CC_SAFE_DELETE(m_spUserDefault);
m_spUserDefault = NULL;
}
CCUserDefault::CCUserDefault()
{
m_spUserDefault = NULL;
}
void CCUserDefault::purgeSharedUserDefault()
{
m_spUserDefault = NULL;
}
bool CCUserDefault::getBoolForKey(const char* pKey)
{
return getBoolForKey(pKey, false);
}
bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
return[[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithUTF8String:pKey]];
}
int CCUserDefault::getIntegerForKey(const char* pKey)
{
return getIntegerForKey(pKey, 0);
}
int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
return [[NSUserDefaults standardUserDefaults] integerForKey: [NSString stringWithUTF8String:pKey]];
}
float CCUserDefault::getFloatForKey(const char* pKey)
{
return getFloatForKey(pKey, 0);
}
float CCUserDefault::getFloatForKey(const char* pKey, float defaultValue)
{
return [[NSUserDefaults standardUserDefaults] floatForKey: [NSString stringWithUTF8String:pKey]];
}
double CCUserDefault::getDoubleForKey(const char* pKey)
{
return [[NSUserDefaults standardUserDefaults] doubleForKey: [NSString stringWithUTF8String:pKey]];
}
double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
return getDoubleForKey(pKey, 0);
}
std::string CCUserDefault::getStringForKey(const char* pKey)
{
return getStringForKey(pKey, "");
}
string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithUTF8String:pKey]];
if (! str)
{
return defaultValue;
}
else
{
return [str UTF8String];
}
}
void CCUserDefault::setBoolForKey(const char* pKey, bool value)
{
[[NSUserDefaults standardUserDefaults] setBool:value forKey:[NSString stringWithUTF8String:pKey]];
}
void CCUserDefault::setIntegerForKey(const char* pKey, int value)
{
[[NSUserDefaults standardUserDefaults] setInteger:value forKey:[NSString stringWithUTF8String:pKey]];
}
void CCUserDefault::setFloatForKey(const char* pKey, float value)
{
[[NSUserDefaults standardUserDefaults] setFloat:value forKey:[NSString stringWithUTF8String:pKey]];
}
void CCUserDefault::setDoubleForKey(const char* pKey, double value)
{
[[NSUserDefaults standardUserDefaults] setDouble:value forKey:[NSString stringWithUTF8String:pKey]];
}
void CCUserDefault::setStringForKey(const char* pKey, const std::string & value)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]];
}
CCUserDefault* CCUserDefault::sharedUserDefault()
{
if (! m_spUserDefault)
{
m_spUserDefault = new CCUserDefault();
}
return m_spUserDefault;
}
bool CCUserDefault::isXMLFileExist()
{
return false;
}
void CCUserDefault::initXMLFilePath()
{
}
// create new xml file
bool CCUserDefault::createXMLFile()
{
return false;
}
const string& CCUserDefault::getXMLFilePath()
{
return m_sFilePath;
}
void CCUserDefault::flush()
{
}
NS_CC_END
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)