일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- swiftlint
- basic
- dart
- Leetcode
- ToDoRim
- reetcode
- tip
- Widget
- flutter
- GIT
- Swift
- IOS
- pubspec.yaml
- protocol
- pubspec
- github
- keyWindow
- algorithm
- SwiftGen
- designPattern
- listview
- xcode
- Extentsion
- enumerations
- OSLog
- toyproject
- UIAccessibility
- COMMIT
- Equatable
- it
- Today
- Total
수니의 개발새발
[iOS/Swift] Keychain으로 안전하게 데이터 저장/반환/삭제하기 (pod 'SwiftKeychainWrapper') 본문
[iOS/Swift] Keychain으로 안전하게 데이터 저장/반환/삭제하기 (pod 'SwiftKeychainWrapper')
개발자 수니 2022. 6. 10. 22:22📌 이번 글은
Keychain으로 안전한 데이터를 저장하는 방법입니다.
이 글에서는 SwiftKeychainWrapper 라이브러리를 사용할 예정입니다.
Keychain
앱에서 사용자 비밀번호, 토큰과 같은 민감함 정보를 저장해야 할 때, 안전하게 저장할 필요가 있습니다.
iOS는 암호화된 컨테이너로 Keychain이 존재하며, 민감함 데이터를 암호화하고 복호화하며 재사용하는 행위를 보다 쉽고 안전하게 사용할 수 있게끔 Keychain Services API를 제공합니다.
SwiftKeychainWrapper
Keychain Services API를 보다 편하고 안전하게 이용할 수 있게끔 해주는 라이브러리 입니다.
GitHub - jrendel/SwiftKeychainWrapper: A simple wrapper for the iOS Keychain to allow you to use it in a similar fashion to User
A simple wrapper for the iOS Keychain to allow you to use it in a similar fashion to User Defaults. Written in Swift. - GitHub - jrendel/SwiftKeychainWrapper: A simple wrapper for the iOS Keychain ...
github.com
SwiftKeychainWrapper 설치
- podfile에 아래 코드 추가 -> 터미널에서 pod install 또는 pod update
pod 'SwiftKeychainWrapper'
Cocoa Pods 참고 포스팅
[iOS/Xcode] CocoaPods(코코아팟) 사용하기
CocoaPods는 Swift 및 Objective-C 코코아 프로젝트의 종속성 관리자입니다. 28,000개가 넘는 라이브러리를 가지고 있으며 170만 개가 넘는 응용 프로그램(앱)에서 사용되고 있습니다. CocoaPod은 프로젝트를
sunidev.tistory.com
사용할 Swift 파일에 import
import SwiftKeychainWrapper
Keychain 저장
// KeychainWrapper.standard.set("저장할 값", forKey: "키")
KeychainWrapper.standard.set("12349891", forKey: "authorization")
Keychain 반환
// KeychainWrapper.standard.string(forKey: "반환할 값의 키")
KeychainWrapper.standard.string(forKey: "authorization")
Keychain 삭제
// KeychainWrapper.standard.removeObject(forKey: "삭제할 키")
KeychainWrapper.standard.removeObject(forKey: "authorization")
👩🏻💻 응용
- 공통 클래스에 Keychain 반환/저장 공용 함수 정의
class Common {
/**
# (E) KeychainKey
- Authors: suni
*/
enum KeychainKey: String {
case authorization
}
/**
# setKeychain
- parameters:
- value : 저장할 값
- keychainKey : 저장할 value의 Key - (E) Common.KeychainKey
- Authors: suni
- Note: 키체인에 값을 저장하는 공용 함수
*/
static func setKeychain(_ value: String, forKey keychainKey: KeychainKey) {
KeychainWrapper.standard.set(value, forKey: keychainKey.rawValue)
}
/**
# getKeychainValue
- parameters:
- keychainKey : 반환할 value의 Key - (E) Common.KeychainKey
- Authors: suni
- Note: 키체인 값을 반환하는 공용 함수
*/
static func getKeychainValue(forKey keychainKey: KeychainKey) -> String? {
return KeychainWrapper.standard.string(forKey: keychainKey.rawValue)
}
/**
# removeKeychain
- parameters:
- keychainKey : 삭제할 value의 Key - (E) Common.KeychainKey
- Authors: suni
- Note: 키체인 값을 삭제하는 공용 함수
*/
static func removeKeychain(forKey keychainKey: KeychainKey) {
KeychainWrapper.standard.removeObject(forKey: keychainKey.rawValue)
}
}
🙋🏻♀️ 참고
'iOS - Swift' 카테고리의 다른 글
[iOS/Swift] iOS13에서 statusBarFrame 경고 해결 (0) | 2024.01.01 |
---|---|
[iOS/Swift] iOS13에서 keyWindow 경고 해결 (0) | 2023.12.22 |
[iOS/Swift] UserDefaults 반환/저장하기 (feat. 자동 로그인 여부 저장) (0) | 2022.06.09 |
[iOS/Swift] OSLog (with. extension) (0) | 2022.04.07 |
[Xcode/Swift] 전처리기, 전처리문(#if DEBUG) (with. Active Compilation Conditions) (0) | 2022.04.01 |