• ios 使用keychain具体方法


    Dictionary  写入:

                if ([self.currentUserAccount length] > 0) {
                    
                    KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc]initWithIdentifier:@"useraccount" accessGroup:nil];
                    if (keychainItem) {
                        
                        if ([[keychainItem objectForKey:kSecAttrAccount] length ] > 0) {
                            
                            //already has data
                            
                            NSMutableArray *accountArray = [[keychainItem objectForKey:kSecAttrAccount] propertyList];
                            if (accountArray && [accountArray isKindOfClass:[NSMutableArray class]] && [accountArray count] > 0)
                            {
                                //just store the different user account
                                //NSUInteger indexOfTheObject = [accountArray indexOfObject: self.currentUserAccount];
                                //if (indexOfTheObject > [accountArray count]) {
                                
                                [accountArray addObject:self.currentUserAccount];
                                [keychainItem setObject:[accountArray description] forKey:(id)kSecAttrAccount];
                                
                                //}
                                for (int index = 0; index < [accountArray count]; index ++) {
                                    
                                    NSLog(@"the value is %@",[accountArray objectAtIndex:index]);
                                }
                            }
                            
                        }else{
                            
                            //has no data
                            NSMutableArray *accountArray = [NSMutableArray arrayWithObject:self.currentUserAccount];
                            [keychainItem setObject:[accountArray description] forKey:(id)kSecAttrAccount];
                            
                        }
                    }
                }

    Dictionary  读取:

        KeychainItemWrapper *tokenKeychainItem = [[[KeychainItemWrapper alloc]initWithIdentifier:@"UserAuthToken" accessGroup:nil]autorelease];
        if (tokenKeychainItem && [[tokenKeychainItem objectForKey:kSecValueData] length ] > 0) {
            
            //[tokenKeychainItem resetKeychainItem];
            
            NSMutableDictionary *dictionary = [[tokenKeychainItem objectForKey:kSecValueData] propertyList];
            
            if (dictionary && [dictionary isKindOfClass:[NSMutableDictionary class]] && [dictionary count] > 0) {
                
                self.tokenValue = [dictionary valueForKey:@"token"];
                //push the view
                SelectChildViewController* pushView = [[SelectChildViewController alloc]init];
                [pushView setTokenValue:self.tokenValue];
                [self.navigationController pushViewController:pushView animated:YES];
                [pushView release];
            }
            ((KidsAppDelegate*)[[UIApplication sharedApplication]delegate]).loginToken = self.tokenValue;
            
            
            //[tokenKeychainItem resetKeychainItem];
            //has token
            /*LoginManager *tmp = [[LoginManager alloc]initWithUserName:@"" withPassword:@""withToken:self.tokenValue];
             self.login = tmp;
             [tmp release];
             [self.login doLogin];*/
            
            
        }

    Array 写入:

    if ([self.currentUserAccount length] > 0) {
                    
                    KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc]initWithIdentifier:@"useraccount" accessGroup:nil];
                    if (keychainItem) {
                        
                        if ([[keychainItem objectForKey:kSecAttrAccount] length ] > 0) {
                            
                            //already has data
                            
                            NSMutableArray *accountArray = [[keychainItem objectForKey:kSecAttrAccount] propertyList];
                            if (accountArray && [accountArray isKindOfClass:[NSMutableArray class]] && [accountArray count] > 0)
                            {
                                //just store the different user account
                                //NSUInteger indexOfTheObject = [accountArray indexOfObject: self.currentUserAccount];
                                //if (indexOfTheObject > [accountArray count]) {
                                
                                [accountArray addObject:self.currentUserAccount];
                                [keychainItem setObject:[accountArray description] forKey:(id)kSecAttrAccount];
                                
                                //}
                                for (int index = 0; index < [accountArray count]; index ++) {
                                    
                                    NSLog(@"the value is %@",[accountArray objectAtIndex:index]);
                                }
                            }
                            
                        }else{
                            
                            //has no data
                            NSMutableArray *accountArray = [NSMutableArray arrayWithObject:self.currentUserAccount];
                            [keychainItem setObject:[accountArray description] forKey:(id)kSecAttrAccount];
                            
                        }
                    }
                }

    Array 读取:

    KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc]initWithIdentifier:@"useraccount" accessGroup:nil];
        self.accountArray = [NSMutableArray array];
        self.lastTimeAccount = @"";
        
        if (keychainItem) {
            
            //[keychainItem resetKeychainItem];
            //first get the old token if found delete //kSecValueData
            if ([[keychainItem objectForKey:kSecAttrAccount] length ] > 0) {
                
                NSMutableArray *orignalArray = [[keychainItem objectForKey:kSecAttrAccount] propertyList];
                
                if (orignalArray && [orignalArray isKindOfClass:[NSMutableArray class]] && [orignalArray count] > 0)
                {
                    //move the repeated item
                    NSInteger index = [orignalArray count] - 1;
                    for (id object in [orignalArray reverseObjectEnumerator]) {
                        if ([orignalArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
                            [orignalArray removeObjectAtIndex:index];
                        }
                        index--;
                    }
                }
                self.accountArray = orignalArray;
                if ([self.accountArray count] > 0) {
                    
                    //self.lastTimeAccount = [self.accountArray objectAtIndex:[self.accountArray count] - 1];
                    self.switchBtn.hidden = NO;
                    
                }
                
            }
            [keychainItem release];
        }

  • 相关阅读:
    初探XML
    Hibernate 由实体类与配置文件的配置关系生成数据库中的表
    利用JSP中的过滤器解决中文乱码问题
    关于iBatis配置xml文件时出现中文注释出错的一个问题(很坑爹.)
    Myeclipse中xml文件里自动提示消失解决办法
    iBatis的基本配置+CRUD操作
    Myeclipse下配置svn
    Hibernate与iBastis 比较(转载)
    本人了解的分页查询
    Hibernate五大核心接口简介
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3963124.html
Copyright © 2020-2023  润新知