objective c - Storing information about users in iOS -
i want store basic information , in cause sensitive data.
the information typically want store is:
username identityid (unique user identityid amazon users) other basic user details email (but no passwords stored)
i read using nsuserdefaults easy hack or see , storing of usernames , identityid isn't enough. should use core data or else? need encrypt core data? data isn't super sensitive i'd still air on side of caution. technically 1 piece of data logged in user , cleared when user logs out.
any simple tutorial on 1 record core data file great.
use key chain store kind of data:
import uikit import security // identifiers let serviceidentifier = "myserivice" let useraccount = "authenticateduser" let accessgroup = "myserivice" // arguments keychain queries let ksecclassvalue = ksecclass.takeretainedvalue() nsstring let ksecattraccountvalue = ksecattraccount.takeretainedvalue() nsstring let ksecvaluedatavalue = ksecvaluedata.takeretainedvalue() nsstring let ksecclassgenericpasswordvalue = ksecclassgenericpassword.takeretainedvalue() nsstring let ksecattrservicevalue = ksecattrservice.takeretainedvalue() nsstring let ksecmatchlimitvalue = ksecmatchlimit.takeretainedvalue() nsstring let ksecreturndatavalue = ksecreturndata.takeretainedvalue() nsstring let ksecmatchlimitonevalue = ksecmatchlimitone.takeretainedvalue() nsstring class keychainservice: nsobject { /** * exposed methods perform queries. * note: feel free play around arguments * these if want able customise * service identifier, user accounts, access groups, etc. */ public class func savetoken(token: nsstring) { self.save(serviceidentifier, data: token) } public class func loadtoken() -> nsstring? { var token = self.load(serviceidentifier) return token } /** * internal methods querying keychain. */ private class func save(service: nsstring, data: nsstring) { var datafromstring: nsdata = data.datausingencoding(nsutf8stringencoding, allowlossyconversion: false) // instantiate new default keychain query var keychainquery: nsmutabledictionary = nsmutabledictionary(objects: [ksecclassgenericpasswordvalue, service, useraccount, datafromstring], forkeys: [ksecclassvalue, ksecattrservicevalue, ksecattraccountvalue, ksecvaluedatavalue]) // delete existing items secitemdelete(keychainquery cfdictionaryref) // add new keychain item var status: osstatus = secitemadd(keychainquery cfdictionaryref, nil) } private class func load(service: nsstring) -> nsstring? { // instantiate new default keychain query // tell query return result // limit our results 1 item var keychainquery: nsmutabledictionary = nsmutabledictionary(objects: [ksecclassgenericpasswordvalue, service, useraccount, kcfbooleantrue, ksecmatchlimitonevalue], forkeys: [ksecclassvalue, ksecattrservicevalue, ksecattraccountvalue, ksecreturndatavalue, ksecmatchlimitvalue]) var datatyperef :unmanaged<anyobject>? // search keychain items let status: osstatus = secitemcopymatching(keychainquery, &datatyperef) let opaque = datatyperef?.toopaque() var contentsofkeychain: nsstring? if let op = opaque? { let retrieveddata = unmanaged<nsdata>.fromopaque(op).takeunretainedvalue() // convert data retrieved keychain string contentsofkeychain = nsstring(data: retrieveddata, encoding: nsutf8stringencoding) } else { println("nothing retrieved keychain. status code \(status)") } return contentsofkeychain } }
usage
keychainservice.savetoken("token") keychainservice.loadtoken()
(from example code: http://matthewpalmer.net/blog/2014/06/21/example-ios-keychain-swift-save-query/)
or use "locksmith", cleaner , easier:
Comments
Post a Comment