2014-04-30 109 views
0

我正在使用SSKeyChain库来存储和访问钥匙串中的密码。我正在iPhone模拟器中测试,我可以编写和访问钥匙串。但是,当我关闭应用程序(双击主页按钮,向上滑动应用程序,所以它不再在后台)我无法访问钥匙串中的数据。我的理解是,除非明确删除,否则这些数据将保留在那里。我错了,还是有可能我没有妥善保存数据?iOS钥匙串访问

第二个问题是,这个数据应该加密还是钥匙串已经处理了?如果应该加密的话,有什么建议?

这里是我的代码:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    /* 
    Let sskeychain know how the keychain values can be accessed - these are the options: 

    kSecAttrAccessibleWhenUnlocked Only accessible when device is unlocked. 
    kSecAttrAccessibleAfterFirstUnlock Accessible while locked. But if the device is restarted it must first be unlocked for data to be accessible again. 
    kSecAttrAccessibleAlways Always accessible. 
    kSecAttrAccessibleWhenUnlockedThisDeviceOnly Only accessible when device is unlocked. Data is not migrated via backups. 
    kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly Accessible while locked. But if the device is restarted it must first be unlocked for data to be accessible again. Data is not migrated via backups. 
    kSecAttrAccessibleAlwaysThisDeviceOnly Always accessible. Data is not migrated via backups. 
    */ 

    [SSKeychain setAccessibilityType:kSecAttrAccessibleWhenUnlocked]; 

    txtUserName = [[UITextField alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-100.0, 200.0, 200.0, 30.0)]; 
    txtUserName.placeholder = @"User Name"; 
    txtUserName.font = [UIFont fontWithName:@"Avenir" size:18.0]; 
    txtUserName.layer.cornerRadius = 8.0f; 
    txtUserName.backgroundColor = [UIColor whiteColor]; 
    txtUserName.layer.borderWidth = 1.0; 
    [self.view addSubview:txtUserName]; 

    txtSignon = [[UITextField alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-100.0, txtUserName.frame.origin.y + txtUserName.frame.size.height + 20.0, 200.0, 30.0)]; 
    txtSignon.placeholder = @"Password"; 
    txtSignon.font = [UIFont fontWithName:@"Avenir" size:18.0]; 
    txtSignon.layer.cornerRadius = 8.0f; 
    txtSignon.backgroundColor = [UIColor whiteColor]; 
    txtSignon.layer.borderWidth = 1.0; 
    txtSignon.secureTextEntry = YES; 
    [self.view addSubview:txtSignon]; 

    UIButton* btnSubmit = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btnSubmit setTitle:@"Submit" forState:UIControlStateNormal]; 
    [btnSubmit addTarget:self action:@selector(storeValues:) forControlEvents:UIControlEventTouchUpInside]; 
    [btnSubmit setFrame:CGRectMake((self.view.frame.size.width/2)-100, txtSignon.frame.origin.y + txtSignon.frame.size.height + 20.0, 200.0, 30.0)]; 
    [self.view addSubview:btnSubmit]; 

    UIButton* btnGetData = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btnGetData setTitle:@"Get Stored Credentials" forState:UIControlStateNormal]; 
    [btnGetData addTarget:self action:@selector(getStoredValues:) forControlEvents:UIControlEventTouchUpInside]; 
    [btnGetData setFrame:CGRectMake((self.view.frame.size.width/2)-100, btnSubmit.frame.origin.y + btnSubmit.frame.size.height + 20.0, 200.0, 30.0)]; 
    [self.view addSubview:btnGetData]; 


} 

- (void) storeValues : (UIButton*) myButton { 

    [self.view endEditing:YES]; 

    //get the saved values 
    NSString* strUserName = txtUserName.text; 
    NSString* strUserPass = txtSignon.text; 

    self.strStoredUserName = strUserName; 

    //pass them along to the keychain 
    [SSKeychain setPassword:strUserPass forService:@"com.sanofi.us" account:strUserName]; 

    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your user name and password were stored in the devices keychain!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
    [alert show]; 

    txtUserName.text = @""; 
    txtSignon.text = @""; 


} 

- (void) getStoredValues : (UIButton*) myButton { 

    NSString* strUserName = self.strStoredUserName; 

    // Access that token when needed 
    NSString* strPassword = [SSKeychain passwordForService:@"com.sanofi.us" account:strUserName]; 

    if (strPassword) { 
     UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Success" message:[NSString stringWithFormat:@"Your credentials stored in the keychain are:\nUsername: %@\nPassword: %@", strUserName, strPassword] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
     [alert show]; 
    } 

} 
+0

1.是的,即使重新安装应用程序后,数据仍然可以访问。 2.举例http://yogeshlolusareapple.blog.com/2014/09/01/key-chain-values-saving-and-retrieving-data/ –

回答

0

此代码看起来是正确的,数据应,除非模拟器正在复位贴敷左右。你能够在设备上测试吗?

要回答第二个问题,根据您的设备和iOS版本,钥匙串密码或者使用3DES或AES进行加密,但确切的实施可能会在将来发生变化。