1

Can anyone give me any link or file, which could be used globally for a project to Store data to NSUserDefault. Which contains method to save dictionary, array,string,integer all.

2 Answers 2

1
  #import <Foundation/Foundation.h>
#import "BSUser.h"


@interface CLServerDataManager : NSObject

+(BOOL) saveServerDataToUserDefaults:(id)data;
+(NSDictionary*) getServerDataFromUserDefaults;

//For language Selection
+(void) setLanguage:(NSString*)LanguageName;
+(NSString*) getLanguage;



+(void) setUserPassword:(NSString*)password;
+(NSString*) getUserPassword;

+(void) setUserID:(NSString*)password;
+(NSString*) getUserID;

+(void) setUserUUID:(NSString*)uuid;
+(NSString*) getUserUUID;


+(void) setUserName:(NSString*)name;
+(NSString*) getUserName;

+(void) setimagedata:(float)data;
+(float) getimagedata;


+(void) setUserProfile:(BSUser*)user;
+(BSUser*) getUserProfile;

+ (void) setApplicationPurchaseStatus:(BOOL)purchaseStatus forUser:(NSString*)fbUserID;
+ (BOOL) getApplicationPurchaseStatusforUser:(NSString*)fbUserID;


+ (void) sethintAndTutorial:(BOOL)hintState;

+ (BOOL) gethintAndtutorial;


+ (void) setDeviceToken:(NSString*)deviceToken;
+ (NSString*) getDeviceToken;


//Methods for clear all data from user defaults
+ (void) resetServerDataManager;
@end

and .m file for this

#import "CLServerDataManager.h"
#import "ClConfig.h"

@implementation CLServerDataManager

+(BOOL) saveServerDataToUserDefaults:(id)data {
//    if (!data) {
//        return NO;
//    }
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//  [defaults setObject:data forKey:USER_ID];
//  [defaults synchronize];
//    return YES;
    return NO;

}

+(NSDictionary*) getServerDataFromUserDefaults {
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//  NSDictionary* rememberMe = [defaults objectForKey:USER_ID];
//    if (rememberMe == nil) {
//        return nil;
//    }
//  return rememberMe;
    return nil;
}


//For the store user select language and again fetch
+(void) setLanguage:(NSString*)LanguageName {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:LanguageName forKey:@"Language"];
    [defaults synchronize];

}

+(NSString*) getLanguage {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* Mylanguage = [defaults objectForKey:@"Language"];
    if (Mylanguage == nil) {
        return nil;
    }
    return Mylanguage;
}


//for store information cache for user's pics download
+(void) setimagedata:(float)data{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithFloat:data] forKey:@"cache"];
    [defaults synchronize];

}
+(float)getimagedata{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    float data = [[defaults objectForKey:@"cache"] floatValue];
    if (data == 0) {
        return 0;
    }
    return data;

}



+(void) setUserPassword:(NSString*)password {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:password forKey:@"password"];
    [defaults synchronize];
}

+(NSString*) getUserPassword {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* rememberMe = [defaults objectForKey:@"password"];
    if (rememberMe == nil) {
        return nil;
    }
    return rememberMe;

}

+(void) setUserID:(NSString*)password {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:password forKey:@"userID"];
    [defaults synchronize];
}

+(NSString*) getUserID {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* rememberMe = [defaults objectForKey:@"userID"];
    if (rememberMe == nil) {
        return nil;
    }
    return rememberMe;
}

+(void) setUserUUID:(NSString*)uuid {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:uuid forKey:@"userUUID"];
    [defaults synchronize];
}

+(NSString*) getUserUUID {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* rememberMe = [defaults objectForKey:@"userUUID"];
    if (rememberMe == nil) {
        return nil;
    }
    return rememberMe;
}


+(void) setUserName:(NSString*)name {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:name forKey:@"userName"];
    [defaults synchronize];
}

+(NSString*) getUserName {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* name = [defaults objectForKey:@"userName"];
    if (name == nil) {
        return nil;
    }
    return name;
}

//store the current login user profile to user defaults

+(void) setUserProfile:(BSUser*)user {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:user] forKey:@"user_profile"];
    [defaults synchronize];

}


+(BSUser*) getUserProfile {
    BSUser* returnObject = nil;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSData *dataRepresentingSavedArray = [defaults objectForKey:@"user_profile"];
    if (dataRepresentingSavedArray != nil)
    {
        returnObject = (BSUser*)[NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
    }
    return returnObject;
}





+ (void) setApplicationPurchaseStatus:(BOOL)purchaseStatus forUser:(NSString*)fbUserID {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithBool:purchaseStatus] forKey:fbUserID];
    [defaults synchronize];
}

+ (BOOL) getApplicationPurchaseStatusforUser:(NSString*)fbUserID {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSNumber* purchased = [defaults objectForKey:fbUserID];
    if (purchased == nil) {
        return NO;
    }
    return [purchased boolValue];
}


#pragma mark Set hint and tutotrial
+ (void) sethintAndTutorial:(BOOL)hintState{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithBool:hintState] forKey:@"hint"];

    [defaults synchronize];

}

+ (BOOL) gethintAndtutorial{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSNumber* hintstate = [defaults objectForKey:@"hint"];

    if (hintstate == nil) {
        return NO;
    }
    return [hintstate boolValue];
}



+ (void) setDeviceToken:(NSString*)deviceToken {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:deviceToken forKey:@"DeviceToken"];
    [defaults synchronize];
}

+ (NSString*) getDeviceToken {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* name = [defaults objectForKey:@"DeviceToken"];
    if (name == nil) {
        return nil;
    }
    return name;
}

+ (void) resetServerDataManager {

    [[self class] setLanguage:nil];

    [[self class] setUserID:nil];
    [[self class] setUserName:nil];
    [[self class] setUserPassword:nil];
    [[self class] setUserProfile:nil];
    [[self class] setUserUUID:nil];

    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        NSString *domainName = [cookie domain];
        NSRange domainRange = [domainName rangeOfString:@"facebook"];
        if(domainRange.length > 0)
        {
            [storage deleteCookie:cookie];
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try below link, You will get thing which you need..

NSUserDefaults

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.