iOS 数据持久化之KeyChain(Swift Demo)

原创blog,转载请注明出处 blog.csdn.net/hello_hwc?viewmode=list

前言:前两篇持久化分别讲到了

NSUserDefaults保存Settings信息Plist保存简单的结构化信息

本文讲解如何保存需要加密的信息。绝大多数情况下都是保存密码。少数情况下需要保存证书等信息。本文以密码为例,讲解如何用iOS SDK原生API来进行KeyChain的操作。 实际开发的过程中,建议使用一些Github的集成库,或者自己写一个KeyChain的库,很简单

源代码提供Swift版本,完整工程下载 CSDN下载 GitHub https://github.com/wenchenhuang/SwiftKeyChainDemo

Demo效果 四个按键对应添加,,更新,获取,删除

Demo的password没有显示黑点,是为了方便查看。

四种操作

---Get----

KeyChain简介

KeyChain是一个加密的容器,通常用来保存密码,证书,和一些需要加密的key。对于iOS来说,每个App有独立的keyChain,每个app只能访问自己的keyChain. 注意:keyChain的访问权限依赖于provisioning file。所以,如果要在应用更新的时候,仍然能够访问之前保存的密码,要保证provisioning file是同一个文件。

KeyChain描述

keyChain是通过字典来描述的,是一组key-value的对。用来描述这个keyChain是为什么样的应用保存什么样的数据,有什么样的访问权限等等。 一个典型的字典

其中

所有的keys可以从以下链接获取 https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/index.html

手把手教你建立Demo App创建一个基于Swift的工程,然后在storyboard上拖拽控件

并且拖拽outlet和action,然后实现UITextFieldDelegate,保证我们点击Return的时候,键盘会消失。这时候的代码如下

import Securityclass ViewController: UIViewController,UITextFieldDelegate{@IBOutlet weak var usernameTextfield: UITextField!@IBOutlet weak var passwordTextField: UITextField!@IBAction func addKeyChainItem(sender: AnyObject) {}@IBAction func updateKeyChainItem(sender: AnyObject) {}@IBAction func getKeyChainItem(sender: AnyObject) {}@IBAction func deleteKeyChainItem(sender: AnyObject) {}override func viewDidLoad() {super.viewDidLoad()usernameTextfield.delegate = selfpasswordTextField.delegate = self// Do any additional setup after loading the view, typically from a nib.}func textFieldShouldReturn(textField: UITextField) -> Bool {textField.resignFirstResponder()return true}}然后,添加几个个辅助方法,减少我们的代码量

创建默认的描述字典

func createDefaultKeyChainItemDic()->NSMutableDictionary{var keyChainItem = NSMutableDictionary()keyChainItem.setObject(kSecClassInternetPassword as NSString, forKey: kSecClass as NSString)keyChainItem.setObject(“blog.csdn.net/hello_hwc”, forKey: kSecAttrServer as NSString)keyChainItem, forKey: kSecAttrAccount as NSString)return keyChainItem}

用UIAlertController提示信息

func alertWithMessage(message:String){var alertController = .Alert)alertController.addAction(.Cancel, handler:nil)))}func alertWithStatus(status:OSStatus){if(status == 0){self.alertWithMessage(“Success”)}else{self.alertWithMessage(“Fail ErrorCode is\(status)”)}}添加KeyChain @IBAction func addKeyChainItem(sender: AnyObject) {var keyChainItem = self.createDefaultKeyChainItemDic()if SecItemCopyMatching(keyChainItem,nil) == noErr{self.alertWithMessage(“User name already exits”)}else{keyChainItem.setObject(self.passwordTextField.text.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion:true)!, forKey: kSecValueData as String)var status = SecItemAdd(keyChainItem, nil)self.alertWithStatus(status)}}更新KeyChain

SecItemUpdate函数用来更新KeyChain,两个参数,第一个参数是描述字典,第二个是包含更新数据的字典

@IBAction func updateKeyChainItem(sender: AnyObject) {var keyChainItem = self.createDefaultKeyChainItemDic()if SecItemCopyMatching(keyChainItem,nil) == noErr{var updateDictionary = NSMutableDictionary() updateDictionary.setObject(self.passwordTextField.text.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion:true)!, forKey:kSecValueData as String)var status = SecItemUpdate(keyChainItem,updateDictionary)self.alertWithStatus(status)}else{self.alertWithMessage(“The keychain doesnot exist”)}}删除keyChain 黄色蓝色或者砖红色,犹如童话世界。

iOS 数据持久化之KeyChain(Swift Demo)

相关文章:

你感兴趣的文章:

标签云: