Apple Keychain 是一个非常流行且功能强大的 Swift 工具,每个 iOS 和 MacOS 用户都在使用。 它可用於保存密码、安全笔记、证书等。总的来说,Keychain 是一个加密数据库,具有相当复杂和强大的 API,当您需要保存敏感的用户数据(例如密码)时,请使用Keychain。
范例 :
struct KeychainItemStorable {
let service: String
let account: String
}
extension KeychainItemStorable {
enum Error: Swift.Error {
case passwordNotFound
case unknownError(String)
case invalidData
}
func readPassword() throws -> String {
var query: [String : Any] = [:]
query[kSecClass as String] = kSecClassGenericPassword
query[kSecAttrService as String] = service
query[kSecAttrAccount as String] = account as AnyObject?
query[kSecMatchLimit as String] = kSecMatchLimitOne
query[kSecReturnAttributes as String] = kCFBooleanTrue
query[kSecReturnData as String] = kCFBooleanTrue
// Try to fetch the existing keychain item that matches the query.
var queryResult: AnyObject?
let status = withUnsafeMutablePointer(to: &queryResult) {
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
}
switch status {
case noErr:
guard let items = queryResult as? [String: Any],
let valueData = items[kSecValueData as String] as? Data,
let valueString = String(data: valueData, encoding: .utf8) else
{ throw Error.invalidData }
return valueString
case errSecItemNotFound:
throw Error.passwordNotFound
default:
throw Error.unknownError(status.description)
}
}
}
优点 :
Plist 文件允许使用键/值约定存储序列化对象。 在 macOS 和 iOS 应用程序中,plist 最常见的用法示例是存储应用程序设置值的 Info.plist 文件。 在 Info.plist 中,我们可以找到应用程序名称、版本号、所需权限信息、Facebook 应用程序信息等。 在我们所有使用 Firebase 的 iOS 应用程序模板中实现的 .plist 文件的另一个示例是 GoogleService -Info.plist 文件,其中包含用户身份验证和数据库访问所需的所有 Firebase API 密钥
在 plist 文件中,您可以存储基本型态资料,例如:
可以使用 Bundle.main.infoDictionary
读取存储在应用程序的 Info.plist 文件中的信息。
如下所示:
let bundleIdentifire = Bundle.main.infoDictionary[kCFBundleIdentifierKey] as? String
范例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FirstName</key>
<string>John</string>
<key>SecondName</key>
<string>Doe</string>
<key>Age</key>
<string>24</string>
</dict>
</plist>
class PlistReader {
func read(fileNamed: String) throws -> [String: Any] {
guard let path = bundle.path(forResource: fileNamed, ofType: "plist"),
let plistData = FileManager.default.contents(atPath: path) else {
throw Error.fileMissing
}
var format = PropertyListSerialization.PropertyListFormat.xml
return try PropertyListSerialization.propertyList(from: plistData,
options: .mutableContainersAndLeaves,
format: &format) as! [String: Any]
}
}
<<: Day26 Plugin 从零开始到上架 - Android总结
>>: Day28. 虽然今年是2021,但我们要做2048(2)
在应用程序开发中,常会利用人工手动去测试系统的正确,不过当遇到较复杂的系统时,手动测试相对耗时且容易...
今天我们会来简单介绍v-if和v-show,并对这两者进行比较,探讨它们的差异在哪里~~ v-if ...
前情提要 倒在路边的我,醒来发现人早已不见,只好回家过节。 (时间来到了,回到家中的午餐後) **我...
大家好..小弟在下我是机械系背景的..... 因为对机械系没有太大的兴趣...所以就半路转换跑道了 ...
此文仅收集便宜的独立服务器,排名不分先后,不定期更新,有不对的或想投稿的请在在评论区评论 Onepr...