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];
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。