Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

Save and Load Data From Keychain ios swift

//**EXTENSION CODE**
import Security
import UIKit

class KeyChain {

    class func save(key: String, data: Data) -> OSStatus {
        let query = [
            kSecClass as String       : kSecClassGenericPassword as String,
            kSecAttrAccount as String : key,
            kSecValueData as String   : data ] as [String : Any]

        SecItemDelete(query as CFDictionary)

        return SecItemAdd(query as CFDictionary, nil)
    }

    class func load(key: String) -> Data? {
        let query = [
            kSecClass as String       : kSecClassGenericPassword,
            kSecAttrAccount as String : key,
            kSecReturnData as String  : kCFBooleanTrue!,
            kSecMatchLimit as String  : kSecMatchLimitOne ] as [String : Any]

        var dataTypeRef: AnyObject? = nil

        let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)

        if status == noErr {
            return dataTypeRef as! Data?
        } else {
            return nil
        }
    }

    class func createUniqueID() -> String {
        let uuid: CFUUID = CFUUIDCreate(nil)
        let cfStr: CFString = CFUUIDCreateString(nil, uuid)

        let swiftString: String = cfStr as String
        return swiftString
    }
}

extension Data {

    init<T>(from value: T) {
        var value = value
        self.init(buffer: UnsafeBufferPointer(start: &value, count: 1))
    }

    func to<T>(type: T.Type) -> T {
        return self.withUnsafeBytes { $0.load(as: T.self) }
    }
}

///**USAGE**
let int: Int = 555
let data = Data(from: int)
let status = KeyChain.save(key: "MyNumber", data: data)
print("status: ", status)
    
if let receivedData = KeyChain.load(key: "MyNumber") {
    let result = receivedData.to(type: Int.self)
    print("result: ", result)
}
Comment

PREVIOUS NEXT
Code Example
Swift :: swift increase int value 
Swift :: To get IPAddress for wifi , wired, and cellular 
Swift :: Swift Assigning and accessing a value from an optional 
Swift :: ios tableview hide empty cells 
Swift :: Swift Nested Function with Parameters 
Swift :: Swift Function With inout Parameters 
Swift :: Swift Library Function 
Swift :: Swift Function overloading with Argument Label 
Swift :: Why Inheritance? 
Swift :: Swift Escape Sequences 
Swift :: convert meter to miles swift 
Swift :: code that detect input swift 
Swift :: swift methods 
Swift :: view rounded corners swift 
Swift :: Swift Syntax of Nested Function 
Swift :: Swift Access Control 
Swift :: swift complete print function syntax 
Swift :: enum Associated Values Swift 
Swift :: underline text in storyboard xcode 
Ruby :: how to rename a table in ruby 
Ruby :: ruby calculate execution time 
Ruby :: ruby constructor 
Ruby :: ruby iterate over array 
Ruby :: rails update without validation 
Ruby :: full error messages rails 
Ruby :: ruby match word in string 
Ruby :: symbol to string ruby 
Ruby :: super vs super() ruby 
Ruby :: meaning of {} in ruby 
Ruby :: ActionController::InvalidAuthenticityToken rails when submitting form 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =