zoukankan      html  css  js  c++  java
  • 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];
        }

  • 相关阅读:
    MySQL存储引擎与索引
    最长公共子序列
    最长递增子序列
    排序算法
    二分查找及其变种
    多线程中锁的种类。
    <LeetCode>136. 只出现一次的数字
    <LeetCode>121. 买卖股票的最佳时机
    Netty 粘包/拆包应用案例及解决方案分析
    微服务
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3963124.html
Copyright © 2011-2022 走看看