swift之保存数据到keychain
访问KeyChain
1.在mac上按下 Command+Space 输入Keychain Access
2.在终端输入security find-generic-password -help
读取配置文件授权
cd ~/Desktop
//ios产看应用的授权来发现他需要的访问类型,应用的授权以编码的形式保存在对应的签名的配置文件中,嘉定已经创建一个配置文件,名为KeycahinTest_Dev.mobileprovision并且保存在桌面
security cms -D -i KeychainTest_Dev.mobileprovision | grep -A12 “Entitlements”
keychain-acess-groups 定义同一开发者开发的应用所共享的钥匙串群组标识
在ios应用中使用key-chain
1.引入框架 import Security 修改Build Setting中Capabilities支持
使用Touch ID验证用户
1.引入LocalAuthentication框架
2.创建LAContext类的实例
3.调用LAContext实例的canEvaluatePolicy:(LAPolicyDeviceOwnerAuthenticaitonWithBiometrics)error:方法确认Touch Id是否可用
4.Touchid可用则使用LAContext的evaluatePolicy:localizedReason:reply:方法验证TouchID的用户
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
@IBOutlet weak var buttonCheckTouchId: UIButton!
@IBOutlet weak var buttonUseTouchId: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
//touchId是否可用
func checkTouchIdAvailability(sender: AnyObject) {
let context = LAContext()
var error: NSError?
let isTouchIdAvailable = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
buttonUseTouchId.isEnabled = isTouchIdAvailable
//Touch ID不可用的
if isTouchIdAvailable == false {
let alertController = UIAlertController(title: "Touch Id", message: "TocuhId is not available", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
@IBAction func userTouchId(sender: AnyObject) {
let context = LAContext()
var error: NSError?
//localizedReason标识在应用中请求用户提供指纹进行验证的文本提示内容
let reason = "please authenticate with Touch id to access your private information"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: { (success: Bool, error: NSError!) in
if success{
//用户已经通过验证
}else{
//用户为通过验证
}
} as! (Bool, Error?) -> Void)
}
}
在钥匙串中存储数据是以键值对方式存储的
kSecCalss 安全存储字符串数据
kSecClassGenericPassword
kSecAttrService 应用bundle标识字符窜
kSecAttrAcount 存储数据的对应键,可以是任意有意义的字符串
kSecValueData 该键的值是一个NSData实例,存放kSecAttrAccount对应的数据
OSStatus类型在xcode中Commond+ shilft + O 输入SecBase 查找errSecSucces,可以看到OSStatus类型
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
//保存到钥匙串
func savetoKeyChain() {
let key = "Full Name"
let value = "Steve Jobs"
let valueData = value.data(using: .utf8, allowLossyConversion: false)
let service = Bundle.main.bundleIdentifier!
let secItem = [
kSecClass as NSString : kSecClassGenericPassword as NSString,
kSecAttrService as NSString : service,
kSecAttrAccount as NSString : key,
kSecValueData as NSString : valueData!
] as NSDictionary
var result:CFTypeRef?
let status = Int(SecItemAdd(secItem, &result))
switch status {
case Int(errSecSuccess):
print("Successfully stored the value")
case Int(errSecDuplicateItem):
print("this item is already saved, Cannot duplicate it")
default:
print("An error occurred with code\(status)")
}
}
//在钥匙串中查找数据
/****查找钥匙串h中的值
SecItemCopyMatching(CFDictionary, UnsafeMutablePointer<CFTypeRef?>?)
1.构建一个字典,天剑kSecClass键,设置键的值来标识查找项的类型。 例如:kSecClassGenericPassword
2.添加kSecAttrService键。取值为查找项服务的字符串,所有应用应采用相同的值,这样任意应用写到钥匙串的数据,其他应用可以访问
3.添加kSecAttrAccount键,取值为钥匙串已存储项对应的键
4.获取特定属性的值:创建修改日期,需要向字典中添加kSecReturnAttributes,并将其值设置为kCFBooleanTrue
如果设置CFDictionary键为 kSecReturnAttributes键,则返回值为nil或CFDictionaryRef隐含类型
如果为kSecReturnData添加到字典,返回类型是CDDataRef
**/
func queryFromKeyChain(){
let keyToSearchfor = "Full Name"
let service = Bundle.main.bundleIdentifier
let query = [kSecClass as NSString : kSecClassGenericPassword as NSString,
kSecAttrAccount as NSString : keyToSearchfor,
kSecAttrService as NSString : service,
kSecReturnAttributes as NSString : kCFBooleanTrue
] as NSDictionary
var valueAttributes : CFTypeRef?
let results = Int(SecItemCopyMatching(query, &valueAttributes))
if results == Int(errSecSuccess) {
let attributes = valueAttributes! as! NSDictionary
let key = attributes[kSecAttrAccount as NSString] as! String
let accessGroup = attributes[kSecAttrAccessGroup as NSString] as! String
let createDate = attributes[kSecAttrCreationDate as NSString] as! NSDate
let modifiedDate = attributes[kSecAttrModificationDate as NSString] as! NSDate
let serviceValue = attributes[kSecAttrService as NSString] as! String
}else{
print("Error happened with code:\(results)")
}
}
func queryDataFromKeyChain(){
let keyToSearchfor = "Full Name"
let service = Bundle.main.bundleIdentifier
let query = [kSecClass as NSString : kSecClassGenericPassword as NSString,
kSecAttrAccount as NSString : keyToSearchfor,
kSecAttrService as NSString : service,
kSecReturnData as NSString : kCFBooleanTrue
] as NSDictionary
var returnedData : CFTypeRef?
let results = Int(SecItemCopyMatching(query, &returnedData))
if results == Int(errSecSuccess) {
let data = returnedData! as! Data
let value = String(data: data, encoding: .utf8)
}else{
print("Error happened with code:\(results)")
}
}
func updateKeyChain() {
let keyToSearchFor = "Full Name"
let service = Bundle.main.bundleIdentifier
let query = [kSecClass as NSString:
kSecClassGenericPassword as NSString,
kSecAttrService as NSString: service,
kSecAttrAccount as NSString : keyToSearchFor,] as NSDictionary
var result: CFTypeRef?
let found = Int(SecItemCopyMatching(query, &result))
if found == Int(errSecSuccess){
let newData = "Mark tremonti".data(using: .utf8, allowLossyConversion: false)
let update = [kSecValueData as NSString: newData!,
kSecAttrComment as NSString : "my comments"] as NSDictionary
let updated = Int(SecItemUpdate(query, update))
if updated == Int(errSecSuccess){
print("Successfully updated the existing value")
readExistingValue();
} else {
print("failed to update the value. error = \(updated)")
}
}else{
print("error happened. Code=\(found)")
}
}
//更新多个值
func readExistingValue() {
}
swift之保存数据到keychain的更多相关文章
- EasyUI使用JSON保存数据
目前来说,使用JSON保存数据比较方便,前台可以不用Test.aspx 页面,可以直接用Html页面,使用.aspx页面的弊端就不在这里熬述. 具体步骤如下: 1.新建一个Html页面,命名为Test ...
- AC中保存数据与查询数据
//保存数据 hui.ajax(function (ret, err) { }, url, {values: {t:"test",m:"Search",c:&q ...
- SpringMVC保存数据到mysql乱码问题
SpringMVC保存数据到mysql乱码问题 乱码问题常见配置 一.web.xml配置过滤器 <filter> <filter-name>encoding-filter< ...
- Android开发学习---android下的数据持久化,保存数据到rom文件,android_data目录下文件访问的权限控制
一.需求 做一个类似QQ登录似的app,将数据写到ROM文件里,并对数据进行回显. 二.截图 登录界面: 文件浏览器,查看文件的保存路径:/data/data/com.amos.datasave/fi ...
- android fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 1.管理Fragment回退栈 类似与Android系统为Acti ...
- Post model至Web Api创建或是保存数据
前一篇<Post model至Web Api>http://www.cnblogs.com/insus/p/4343538.html中,使用Post来从Web Api获取数据.由于Post ...
- SharedPreferences保存数据
1.使用SharedPreferences保存数据方法如下: //实例化SharedPreferences对象(第一步) SharedPreferences mySharedPreferences= ...
- Android 分享一个SharedPreferences的工具类,方便保存数据
我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPrefe ...
- mysql保存数据提示1366 Incorrect string value: ‘\xF0\x9F\x98\x8A\xF0\x9F…’ 解决
在保存数据时我们如果页面编辑与数据库字段编码不一样或字符集超出你了mysql数据库中的字符类型就有可能出一\\xF0\\x9F\\x98\\x8A\\xF0\\x9F提示了,下面我来简单的解决方法. ...
随机推荐
- Jquery属性操作、添加类
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [PHP] php作为websocket的客户端实时读取推送日志文件
首先要使用composer来下载一个第三方扩展就可以实现php的websocket客户端,直接在当前目录生成下composer.json文件就可以了composer require textalk/w ...
- uniapp属性插值报错Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead.
解决方法: 因为vue 2.x不支持对属性使用插值{{}}的方式赋值,所以要使用v-bind指令(或简写“:”)来指定属性. v-bind指令 v-bind:id="item.id" ...
- SQLlite的olestr
关于SQLite的connection string说明:http://www.connectionstrings.com/sqlite/ SQLite GUI客户端列表:http://www.sql ...
- 接口测试基础----postman、jmeter
一,什么是接口 接口一般接口分两种: 系统对外接口:与外部系统对接的接口,用来获取或者传递数据给外部系统 系统内部接口:系统模块.方法之间用来获取或者传递数据的接口 二.接口分类 webservice ...
- Golang的标准输入输出
Golang的标准输入输出 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在很多情况下,我们希望用户通过键盘输入一个数值,存储到某个变量中,然后将该变量的值取出来,进行操作.这时候 ...
- POJ 1061:青蛙的约会
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 95878 Accepted: 17878 Descripti ...
- CentOS 6.8 32位 安装mysql8
1.清理掉之前安装过的mysql rpm -qa | grep mysql mysql-libs-5.1.52-1.el6_0.1.x86_64 yum remove mysql-libs-5.1.5 ...
- http请求的过程及潜在的性能优化点
web前端的看富于部署过程 开发者将开发的代码发布到远程的服务器(webserver/cdn),用户通过访问浏览器输入相应的网址,浏览器向远程服务器发送请求,动态的增量式的加载资源 web前端就是一个 ...
- 3分钟学会Python 针对Excel操作
1.python 读取Excel # -*- coding: utf-8 -*- import xlrd import os,sys reload(sys) sys.setdefaultencodin ...