So this may be because UserDefaults are getting reset after a period of time, maybe because I update the OS frequently, but a released version of my app after a period of time will crash on my login screen, probably where I check for saved credentials. Reinstalling the app fixes the issue. Console returns a lot of :
Unsupported use of UIKit view-customization API off the main thread. -setAlignsToKeyboard: sent to _UIAlertControllerView: 0x125b2d070; frame = (0 0; 414 896); layer = CALayer: 0x283f652e0
Cannot be called with asCopy = NO on non-main thread.
And my system logs show an error on this thread:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 4
Application Specific Information:
abort() called
...
Thread 4 name: Dispatch queue: com.apple.NSURLSession-delegate
Thread 4 Crashed:
0 libsystem_kernel.dylib 0x00000001bfb4e414 0x1bfb26000 + 164884
1 libsystem_pthread.dylib 0x00000001dd6a8b50 0x1dd6a6000 + 11088
2 libsystem_c.dylib 0x000000019b027b74 0x19afb1000 + 486260
3 libc++abi.dylib 0x00000001a6d1acf8 0x1a6d07000 + 81144
4 libc++abi.dylib 0x00000001a6d0be4c 0x1a6d07000 + 20044
5 libobjc.A.dylib 0x00000001a6c14f64 0x1a6c0e000 + 28516
6 libc++abi.dylib 0x00000001a6d1a0e0 0x1a6d07000 + 78048
7 libc++abi.dylib 0x00000001a6d1a06c 0x1a6d07000 + 77932
8 libdispatch.dylib 0x00000001917eddc4 0x1917ea000 + 15812
9 libdispatch.dylib 0x00000001917f510c 0x1917ea000 + 45324
10 libdispatch.dylib 0x00000001917f5c90 0x1917ea000 + 48272
11 libdispatch.dylib 0x00000001917ffd78 0x1917ea000 + 89464
12 libsystem_pthread.dylib 0x00000001dd6a9814 0x1dd6a6000 + 14356
13 libsystem_pthread.dylib 0x00000001dd6b076c 0x1dd6a6000 + 42860
Here is some code that I use to check saved credentials. In the backend API I cannot see any login attempts. If it helps, I can see this viewcontroller for a split second right before it crashes. Not sure if that rules out viewdidload.
override func viewDidLoad() {
super.viewDidLoad()
addLoadingSpinner()
bLogIn.titleLabel?.font = UIFont.setToVoiceLight()
self.tfCustomerID.delegate = self
self.tfUsername.delegate = self
self.tfPassword.delegate = self
tfUsername.keyboardType = .default
tfPassword.textContentType = .password
setupUI()
if UserDefaults.standard.string(forKey: UserDefaultsKeys.session) != nil
&& UserDefaults.standard.string(forKey: UserDefaultsKeys.savedCred) == "True" {
login_session = UserDefaults.standard.string(forKey: UserDefaultsKeys.session)!
check_session()
}
else if UserDefaults.standard.string(forKey: UserDefaultsKeys.savedCred) == "True"
&& UserDefaults.standard.string(forKey: UserDefaultsKeys.usesBiometrics) == "True" {
promptTouchOrFaceID()
}
removeLoadingSpinner()
}
override func viewWillAppear(_ animated: Bool) {
AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
cbSave.isChecked = UserDefaults.standard.string(forKey: UserDefaultsKeys.savedCred) == "True"
addObservers()
}
func check_session() {
UserDefaults.standard.set(login_session, forKey: "session")
UserInfo.access_token = UserDefaults.standard.string(forKey: "session")!
var request = URLRequest(url: NSURL(string: checksession_url)! as URL)
request.httpMethod = .GET
request.addValues(...)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode == 200 {
//NEVER GETTING TO API
DispatchQueue.main.async {
...
}
} else {
self.promptTouchOrFaceID()
UserDefaults.standard.set(nil, forKey: UserDefaultsKeys.session)
}
}
task.resume()
}
Thanks in advance for any advice. Finding this to be pretty difficult to solve.
Selecting any option will automatically load the page