iOS偷懒方法怎么去保存数据到本地

#import <Foundation/Foundation.h>

@interface LXPTokenManager : NSObject

#pragma mark - 获得企业TOKEN
+(NSString *)accessToken;

#pragma mark - 保存TOKEN
+(void)saveToken:(NSString *)token;

#pragma mark - 判断是否有TOKEN
+(BOOL)hasAvalibleToken;

#pragma mark - 获得企业sercet
+(NSString *)accessSercet;

#pragma mark - 保存TOKEN
+(void)saveSercet:(NSString *)sercet;

#pragma mark - 判断是否有TOKEN
+(BOOL)hasAvalibleSercet;

//异地登录
+(void)exit;
// 清空账号的id

+(void)clearChuangquID;
//清空账号
+(void)clearChuangquRooter;

//清空密码;
+(void)clearChuangqupassword;

//清空
+(void)clearchuangquall;
 
 

#import "LXPTokenManager.h"
NSString *const TOKEN_KEY = @"token";
NSString *const Secr_Cet = @"sercet";

@implementation LXPTokenManager
//获得token
+(NSString *)accessToken
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *token = [userDefaults objectForKey:TOKEN_KEY];
    [userDefaults synchronize];
    return token;
}
//保存token
+(void)saveToken:(NSString *)token {
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    [userDefaults setObject:token forKey:TOKEN_KEY];
    [userDefaults synchronize];
}
//判断是否有可用的token
+(BOOL)hasAvalibleToken {
    
    NSUserDefaults *userDefalts = [NSUserDefaults standardUserDefaults];
    
    NSString *token = [userDefalts objectForKey:TOKEN_KEY];
    if (token) {
        
        return YES;
    }else{
        
        return NO;
    
    }
   
    

}


#pragma mark - 获得企业sercet
+(NSString *)accessSercet{

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *sercet = [userDefaults objectForKey:Secr_Cet];
    [userDefaults synchronize];
    return sercet;


}

#pragma mark - 保存TOKEN
+(void)saveSercet:(NSString *)sercet{

    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    [userDefaults setObject:sercet forKey:Secr_Cet];
    [userDefaults synchronize];
}

#pragma mark - 判断是否有TOKEN
+(BOOL)hasAvalibleSercet{

    NSUserDefaults *userDefalts = [NSUserDefaults standardUserDefaults];
    
    NSString *sercet = [userDefalts objectForKey:Secr_Cet];
    if (sercet) {
        
        return YES;
    }else{
        
        return NO;
        
    }
    


}
//异地登录
+(void)exit{
    
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"帐号异地登录!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    
}
//清空
#pragma mark - 清空本地的数据库数据
<pre name="code" class="objc">#pragma mark - 保存token
                    
                    NSDictionary *token = data[@“xxx"];
                    
                    [LXPTokenManager saveToken:token[@“xxx"]];
                     [LXPTokenManager saveSercet:token[@“xxx"]];

+(void)clearchuangquall{ NSUserDefaults *UserLoginState = [NSUserDefaults standardUserDefaults]; [UserLoginState removeObjectForKey:@“xxx"]; [UserLoginState synchronize]; NSUserDefaults *UserName = [NSUserDefaults standardUserDefaults]; [UserName removeObjectForKey:@"xxx"]; [UserName synchronize];

 
 
}
    
如果拿到后它数据可以调用以上类方法

#pragma mark - 保存token
                    
                    NSDictionary *token = data[@“xxx"];
                    
                    [LXPTokenManager saveToken:token[@“xxx"]];
                     [LXPTokenManager saveSercet:token[@“xxx"]];
 
 
其他同理 



你可能感兴趣的:(iOS偷懒方法怎么去保存数据到本地)