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

  • 相关阅读:
    正则表达式匹配可以更快更简单 (but is slow in Java, Perl, PHP, Python, Ruby, ...)
    ++i? i++? i+=1? i=i+1? 何必纠结?
    数独题的生成与解决方法
    VIM常用设置
    我的“MIT Challenge”
    NDK开发之javaVM
    十二月寒冬
    Linux epoll 笔记(高并发事件处理机制)
    Linux之我见
    半夜惊醒
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3963124.html
Copyright © 2020-2023  润新知