Skip to content

Commit

Permalink
Clear keyChain after fresh install
Browse files Browse the repository at this point in the history
  • Loading branch information
xxi511 committed Jul 30, 2023
1 parent cf482cd commit b322695
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Ptt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
3EA6F1B629B386A2005D8B09 /* ComposeArticleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EA6F1B529B386A2005D8B09 /* ComposeArticleViewController.swift */; };
3EA6F1B829B386F1005D8B09 /* ComposeArticleViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EA6F1B729B386F1005D8B09 /* ComposeArticleViewModel.swift */; };
3EA6F1BA29B388AF005D8B09 /* ComposeArticleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EA6F1B929B388AF005D8B09 /* ComposeArticleView.swift */; };
3EB974B82A7685B8003A8F29 /* UserDefaultItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EB974B72A7685B8003A8F29 /* UserDefaultItem.swift */; };
3EC2CB2229BB53AC00E70A45 /* BoardsTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EC2CB2129BB53AC00E70A45 /* BoardsTableViewCell.swift */; };
3EC2CB2429BB542C00E70A45 /* UITableViewCell+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EC2CB2329BB542C00E70A45 /* UITableViewCell+Extension.swift */; };
3EC2CB2629BB57B000E70A45 /* UserNumberView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EC2CB2529BB57B000E70A45 /* UserNumberView.swift */; };
Expand Down Expand Up @@ -184,6 +185,7 @@
3EA6F1B529B386A2005D8B09 /* ComposeArticleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeArticleViewController.swift; sourceTree = "<group>"; };
3EA6F1B729B386F1005D8B09 /* ComposeArticleViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeArticleViewModel.swift; sourceTree = "<group>"; };
3EA6F1B929B388AF005D8B09 /* ComposeArticleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeArticleView.swift; sourceTree = "<group>"; };
3EB974B72A7685B8003A8F29 /* UserDefaultItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaultItem.swift; sourceTree = "<group>"; };
3EC2CB2129BB53AC00E70A45 /* BoardsTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoardsTableViewCell.swift; sourceTree = "<group>"; };
3EC2CB2329BB542C00E70A45 /* UITableViewCell+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableViewCell+Extension.swift"; sourceTree = "<group>"; };
3EC2CB2529BB57B000E70A45 /* UserNumberView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserNumberView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -341,6 +343,7 @@
children = (
94EB641925AD2FF800BF69AB /* KeyChainItem.swift */,
3EC2CB2C29BB8DAC00E70A45 /* Direction.swift */,
3EB974B72A7685B8003A8F29 /* UserDefaultItem.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand Down Expand Up @@ -974,6 +977,7 @@
EA41D65D243EF51A0052F4A1 /* FBPageViewController.swift in Sources */,
CD6C1C1926AD783100EBFEA9 /* CreateArticle.swift in Sources */,
9B564C65256AA83100787FD7 /* TabBarView.swift in Sources */,
3EB974B82A7685B8003A8F29 /* UserDefaultItem.swift in Sources */,
3E655288255960910026290B /* FullArticle.swift in Sources */,
3E6552822559604C0026290B /* APIClientProtocol.swift in Sources */,
3E8D8C83258A511600346690 /* LoginToken.swift in Sources */,
Expand Down
9 changes: 9 additions & 0 deletions Ptt/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
clearKeychainAfterFreshInstall()
return true
}

Expand Down Expand Up @@ -58,4 +59,12 @@ private extension AppDelegate {
coordinatorFactory: CoordinatorFactory()
)
}

func clearKeychainAfterFreshInstall() {
let freshInstall: Bool = !(UserDefaultItem.getValue(for: .alreadyInstalled) ?? false)
if freshInstall {
KeyChainItem.shared.clear()
UserDefaultItem.set(value: true, for: .alreadyInstalled)
}
}
}
14 changes: 14 additions & 0 deletions Ptt/Common/Utilities/KeyChainItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,18 @@ final class KeyChainItem: PTTKeyChain {
let status = SecItemDelete(query as CFDictionary)
return status == errSecSuccess
}

func clear() {
let secItemClasses = [
kSecClassGenericPassword,
kSecClassInternetPassword,
kSecClassCertificate,
kSecClassKey,
kSecClassIdentity
]
for secItemClass in secItemClasses {
let dictionary = [kSecClass as String:secItemClass]
SecItemDelete(dictionary as CFDictionary)
}
}
}
23 changes: 23 additions & 0 deletions Ptt/Common/Utilities/UserDefaultItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// UserDefaultItem.swift
// Ptt
//
// Created by AnsonChen on 2023/7/30.
// Copyright © 2023 Ptt. All rights reserved.
//

import Foundation

struct UserDefaultItem {
enum Key: String {
case alreadyInstalled
}

static func set(value: Bool, for key: Key) {
UserDefaults.standard.set(value, forKey: key.rawValue)
}

static func getValue<T>(for key: Key) -> T? {
UserDefaults.standard.value(forKey: key.rawValue) as? T
}
}

0 comments on commit b322695

Please sign in to comment.