I am working on a project where I use CLGeocoder to determine the placemarks for a particular location.
Very simple call like this.
CLGeocoder().geocodeAddressString(location) { (placemarks, error) in
if let error = error {
print(error.localizedDescription)
}
Instead of printing out the NSError's localizedDescription, I would like to be able to capture the CLError code and respond accordingly. For example, if the location can't be found, my localizedDescription prints
The operation couldn’t be completed. (kCLErrorDomain error 8.)
How can I, in Swift determine the CLError code so that I do not have to switch on some localized description that will change based on user's locale?
Can this be done?
I only have a couple of cases that I want to switch on.
You need to cast the error as CLError and then switch the error code:
if let error = error as? CLError {
switch error.code {
case .locationUnknown:
print("locationUnknown: location manager was unable to obtain a location value right now.")
case .denied:
print("denied: user denied access to the location service.")
case .promptDeclined:
print("promptDeclined: user didn’t grant the requested temporary authorization.")
case .network:
print("network: network was unavailable or a network error occurred.")
case .headingFailure:
print("headingFailure: heading could not be determined.")
case .rangingUnavailable:
print("rangingUnavailable: ranging is disabled.")
case .rangingFailure:
print("rangingFailure: a general ranging error occurred.")
default : break
}
}
Related
I'm making a request to my server with AlamoFire, and I have a setup something like this, where "self.error" is a String variable.
session.request("https://localhost:3000/my-route", method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
switch response.result {
case .success(let result):
print("nice", result)
case .failure(let err):
self.error = err //self.error is a String state variable
print("failure")
}
}
However, I get the (sensible) error "Cannot assign value of type 'AFError' to type 'String'", because I'm trying to set self.error = err, and err is of type AFError, whereas self.error is a String state variable.
I realize I could just do something like self.error = "There was an error", but I'd like to provide the user with some feedback as to what the error actually was, and I can't seem to find the correct properties (i.e. something like err.stringValue, err.description, etc.) to do so. Seems like there's some source code here https://github.com/Alamofire/Alamofire/blob/master/Source/AFError.swift but I'm not sure how this relates to the string value I'd like to obtain.
Any help here would be much appreciated.
For such cases you can always use String interpolation e.g. self.error = "\(err)", but this is not a good idea because err is not just a string and could contain something like:
sessionTaskFailed(error: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x280e37750 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <78576E38-D021-4ADC-87D4-1C9D81FF0E3A>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <78576E38-D021-4ADC-87D4-1C9D81FF0E3A>.<1>" ), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=https://localhost:3000/my-route, NSErrorFailingURLKey=https://localhost:3000/my-route, _kCFStreamErrorDomainKey=1})
Like #Menaim already mentioned, you can use err.localizedDescription, then you could get something like URLSessionTask failed with error: Could not connect to the server.
Generally, I would not pass the error directly to the user. This depends at your users, but do they really should know what e.g. URLSessionTask is?
In my opinion you should define error string for user, that every user can understand and you should log err.localizedDescription to the console, so you can know what is happening.
If your app supports many languages, you can define localizable.strings files and translate the errors.
If you do this print("failure"), every time you have an error, how can you distinguish the errors?
My suggestion would be something like:
case .failure(let err):
self.error = "There was an error connecting to the server"
print("Failed to connect to backend: \(err.localizedDescription)")
You can try err.localizedDescription or you can get the error in the completion of the function and return it completion(error)
I am working out how to use the Contacts framework, however some fairly simple code to create a contact is failing with an unexpected result. This is my code:
let Store = CNContactStore()
Store.requestAccess(for: .contacts, completionHandler:{ success, error in
if success {
let Contact = CNMutableContact()
Contact.givenName = "Dave"
Contact.familyName = "Nottage"
let SaveRequest = CNSaveRequest()
SaveRequest.add(Contact, toContainerWithIdentifier: nil)
do {
try Store.execute(SaveRequest)
print("Success")
}
catch let error as NSError {
print(error.localizedDescription)
}
} else {
print("No access")
}
})
..and this is the result:
2019-02-22 10:30:56.050344+1030 ContactsTest[30329:25254955] [default] Unable to load Info.plist exceptions (eGPUOverrides)
2019-02-22 10:30:57.973724+1030 ContactsTest[30329:25254955] Could not get real path for Address Book lock folder: open() for F_GETPATH failed.
2019-02-22 10:30:57.973954+1030 ContactsTest[30329:25254955] Unable to open file lock: <ABProcessSharedLock: 0x600001752ac0: name=(null), lockFilePath=(null), localLock=<NSRecursiveLock: 0x600002914a80>{recursion count = 0, name = nil}, fileDescriptor=-1> Error Domain=NSPOSIXErrorDomain Code=14 "Bad address" UserInfo={ABFileDescriptor=-1}
The operation couldn’t be completed. (Foundation._GenericObjCError error 0.)
Any ideas on what might be causing this?
Edit: Note also that this is being compiled for macOS 10.14 SDK, and is running on macOS 10.14.3
The answer is to check the Contacts checkbox of App Data in the App Sandbox section in Capabilities and turn on the switch for App Sandbox.
Make sure you added key NSContactsUsageDescription in Info.plist.
Please refer to link.
Important
An iOS app linked on or after iOS 10.0 must include in its Info.plist
file the usage description keys for the types of data it needs to
access or it will crash. To access Contacts data specifically, it must
include NSContactsUsageDescription.
Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!)
{ (user, error) in
if error != nil {
print(error!)
self.warningLabel.isHidden = false;
self.passwordTextField.text = "";
} else {
print("Log in succesful")
self.performSegue(withIdentifier: "welcomeSeg", sender: self)
}
}
Whenever I sign in or sign up a user I just print a generic warning label instead of the actual issue. I print the error I receive and it's too verbose to show to the user.
Error Domain=FIRAuthErrorDomain Code=17009 "The password is invalid or the user does not have a password." UserInfo={NSLocalizedDescription=The password is invalid or the user does not have a password., error_name=ERROR_WRONG_PASSWORD}
Error Domain=FIRAuthErrorDomain Code=17008 "The email address is badly formatted." UserInfo={NSLocalizedDescription=The email address is badly formatted., error_name=ERROR_INVALID_EMAIL}
Is there any way to fetch the error code so I can be more specific with my error messages? I've looked through the documentation but have been unsuccessful in coming up with anything.
I would recommend creating an AuthErrorCode object (provided by the Firebase SDK) from the error you receive and using that as you see fit. If I remember correctly, AuthErrorCode is an enum with cases like .wrongPassword, .invalidEmail, etc.
A simple pseudocode example:
if error != nil {
if let error = AuthErrorCode(rawValue: error?.code) {
switch error {
case .wrongPassword:
// do some stuff with the error code
}
}
Also, I feel your pain. I've found that the Swift SDK documentation lags quite a bit when changes come along.
Trying to write a unit test in Swift 4 for an already working method and having trouble getting the correct result. My current approach is:
let expected = expectation(description: "Obtain access keys")
let keys = sut.retrieveAccessKeys()
//sleep(5)
if keys != nil {
expected.fulfill()
} else {
XCTFail("ERROR: Failed obtaining access keys")
}
waitForExpectations(timeout: 5) { error in
let myError = error as? Error
print("ERROR: \(String(describing: myError?.localizedDescription))")
}
}
The sut makes a network call via a completion handler if the values don't exist within the keychain. The outputted error I get is:
Error: The operation couldn’t be completed. (com.apple.XCTestErrorDomain error 0.)
I've tried various ways including adding in sleep(5). sut. retrieveAccessKeys() does work and the resulting keys contains an optional tuple.
I'm not able to understand why neither print lines are executed when running the following code:
ref.child("schools/\(schoolTextField.text!)/settings/pin").observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot.value)
}, withCancel: { (error) in
print(error.localizedDescription)
})
It used to work, until I made some changes regarding firebase authentication, but I don't see why the withCancel block is not executed!
How do I catch whatever error is occuring here? No error-message is printed in the log.
EDIT:
I found a similar question here which suggests that the problem might be related to the authentication after all. In appDelegate's didFinishLaunchingWithOptions I check if the user auth-token exists in Firebase Auth:
Auth.auth().currentUser?.getIDTokenForcingRefresh(true, completion: { (response, error) in
guard error == nil, let uid = response else {
print(error)
return
}
...
})
In current case there is an error, which is printed:
Error Domain=FIRAuthErrorDomain Code=17011 "There is no user record corresponding to this identifier. The user may have been deleted."
I'm now also getting these errors printed in the log:
[Common] _BSMachError: port b43b; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
[Common] _BSMachError: port b43b; (os/kern) invalid name (0xf) "Unable to deallocate send right"
Try this way:
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
print("SNAP: \(snap)")
}
Also The error code 17011 is a user not found error see FIRAuthErrorCode for more info also it's better you create a Dataservice class to handle your interaction with firebase see Creating A Reusable Firebase Data Service for more info.