-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update param deviceId when registerDeviceToken
- Loading branch information
Arief Nur Putranto
committed
Nov 6, 2024
1 parent
69c2cf4
commit 859bdca
Showing
6 changed files
with
122 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// QKeychainAccess.swift | ||
// QiscusCore | ||
// | ||
// Created by arief nur putranto on 05/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class QKeychainAccess { | ||
|
||
func addKeychainData(itemKey: String, itemValue: String) throws { | ||
guard let valueData = itemValue.data(using: .utf8) else { | ||
print("Keychain: Unable to store data, invalid input - key: \(itemKey), value: \(itemValue)") | ||
return | ||
} | ||
|
||
//delete old value if stored first | ||
do { | ||
try deleteKeychainData(itemKey: itemKey) | ||
} catch { | ||
print("Keychain: nothing to delete...") | ||
} | ||
|
||
let queryAdd: [String: AnyObject] = [ | ||
kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrAccount as String: itemKey as AnyObject, | ||
kSecValueData as String: valueData as AnyObject, | ||
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked | ||
] | ||
let resultCode: OSStatus = SecItemAdd(queryAdd as CFDictionary, nil) | ||
|
||
if resultCode != 0 { | ||
print("Keychain: value not added - Error: \(resultCode)") | ||
} else { | ||
print("Keychain: value added successfully") | ||
} | ||
} | ||
|
||
func deleteKeychainData(itemKey: String) throws { | ||
let queryDelete: [String: AnyObject] = [ | ||
kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrAccount as String: itemKey as AnyObject | ||
] | ||
|
||
let resultCodeDelete = SecItemDelete(queryDelete as CFDictionary) | ||
|
||
if resultCodeDelete != 0 { | ||
print("Keychain: unable to delete from keychain: \(resultCodeDelete)") | ||
} else { | ||
print("Keychain: successfully deleted item") | ||
} | ||
} | ||
|
||
func queryKeychainData (itemKey: String) throws -> String? { | ||
let queryLoad: [String: AnyObject] = [ | ||
kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrAccount as String: itemKey as AnyObject, | ||
kSecReturnData as String: kCFBooleanTrue, | ||
kSecMatchLimit as String: kSecMatchLimitOne | ||
] | ||
var result: AnyObject? | ||
let resultCodeLoad = withUnsafeMutablePointer(to: &result) { | ||
SecItemCopyMatching(queryLoad as CFDictionary, UnsafeMutablePointer($0)) | ||
} | ||
|
||
if resultCodeLoad != 0 { | ||
print("Keychain: unable to load data - \(resultCodeLoad)") | ||
return nil | ||
} | ||
|
||
guard let resultVal = result as? NSData, let keyValue = NSString(data: resultVal as Data, encoding: String.Encoding.utf8.rawValue) as String? else { | ||
print("Keychain: error parsing keychain result - \(resultCodeLoad)") | ||
return nil | ||
} | ||
return keyValue | ||
} | ||
} |