首页 > 代码库 > ios 使用keychain具体方法

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 = http://www.mamicode.com/[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];
    }

 

ios 使用keychain具体方法