I have a struct that holds an instance of UINavigationController:
struct NavigationController {
static let shared = UINavigationController()
}
I use NavigationController.shared to push and pop ViewControllers around the app, rather than using the ViewController's .navigationController property.
The issue I'm having is that when I pop I get new instances of my previous ViewController, this is my hierarchy:
(0) UIWindow
|
---- (1) NavigationController (is set as the UIWindow.rootViewController)
|
---- (2) UITabBarController (is set with NavigationController.shared.setViewControllers)
|
---- (3) ViewController (HomeVC) (is the first tab of the UITabController)
|
---- (4) ViewController (ScanVC) (is pushed into the stack by NavigationController.shared.pushViewController)
---- (5) ViewController (NotificationsVC)
---- (6) ViewController (SettingsVC)
I put a print statement in my HomeVC in the viewDidLoad method
My understanding is that the viewDidLoad should only be called once in the lifecycle of a ViewController
When I go back to the HomeVC from the ScanVC then the print always gets triggered which means I have a new instance of the HomeVC
This is the print statement I created inside the viewDidLoad method:
print("\(#function) View Did Load, instance: \(self)")
Here's the output from going back and forth from the HomeVC to ScanVC:
viewDidLoad() View Did Load, instance: <HomeVC: 0x118db0000>
viewDidLoad() View Did Load, instance: <HomeVC: 0x118db3100>
viewDidLoad() View Did Load, instance: <HomeVC: 0x118db0700>
Any one has any suggestions on how to fix this? Because ideally going back to the HomeVC should not instantiate a new ViewController.
I tested this on a small test project and viewDidLoad would only be triggered once when the ViewController was instantiated.
Post
Replies
Boosts
Views
Activity
I was basically saving items into the Keychain with the following query dictionary:
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
kSecValueData as String: value,
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
]
Where key is a String value and value is a Data that used to be a String.
I was getting the following error:
code: -25299
description: The specified item already exists in the keychain
After a lot of digging in I saw that I needed to add kSecAttrService to the dictionary and after that it all started working. The service value is a String value.
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: key,
kSecValueData as String: value,
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
]
These were the articles that suggested adding the kSecAttrService parameter:
https://stackoverflow.com/a/11672200
https://stackoverflow.com/a/58233542
But in the same code base I found that other developers were saving using a dictionary similar to the one I first provided and it works:
var query: [String : Any] = [
kSecClass as String : kSecClassGenericPassword as String,
kSecAttrAccount as String : key,
kSecValueData as String : data
]
I don't know how to explain why my first implementation didn't work even though it was similar to what was already in the code base but the second approach worked well.
Regardless of the query dictionary, this is how I'm saving things:
static func save(value: Data, key: String, service: String) -> KeyChainOperationStatus {
logInfo("Save Value - started, key: \(key), service: \(service)")
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: key,
kSecValueData as String: value,
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
]
// Remove any existing key
let cleanUpStatus = SecItemDelete(query as CFDictionary)
let cleanUpStatusDescription = SecCopyErrorMessageString(cleanUpStatus, nil)?.asString ?? "__cleanup_status_unavailable"
logInfo("Save Value - cleanup status: \(cleanUpStatus), description: \(cleanUpStatusDescription)")
guard cleanUpStatus == errSecSuccess || cleanUpStatus == errSecItemNotFound else {
logError("Save Value - Failed cleaning up KeyChain")
return .cleanupFailed(code: cleanUpStatus)
}
// Add the new key
let saveStatus = SecItemAdd(query as CFDictionary, nil)
let saveStatusDescription = SecCopyErrorMessageString(saveStatus, nil)?.asString ?? "__save_status_unavailable"
logInfo("Save Value - save status [\(saveStatus)] : \(saveStatusDescription)")
guard saveStatus == errSecSuccess else {
logError("Save Value - Failed saving new value into KeyChain")
return .savingFailed(code: saveStatus)
}
return .successs
}
Hi,
I've implemented the FamilyActivityPicker and I also noticed that it is the same picker that we get when we go to Setttings > Screen Time > App Limits > Add Limit.
When you tap on a given row, it will present all apps that are in that category. If you press the check mark on Category then on the Category row will be updated by showing the checkmark as selected and a "All" text to the right side, and on the app rows from that category the check marks will also be marked as selected.
This behavior is not consistent when I implement the FamilyActivityPicker. If I go through the same process the app rows won't be shown as selected.
Any suggestions on how to make this work? I'm attaching screen shots to illustrate my point.
Settings App
My FamilyActivityPicker Implementation