I need the authentication via TouchID and FaceID and various "else" requests in my app. I managed to integrate it, so that after pressing the "button" to proceed, you move on to another VIEW.
The problem is that if the "cancel" item is pressed, however, the button that is connected to the next VIEW continues to work. I would like if the user presses "cancel" it will be shown on the home page. The Button is connected via Main.Storyboard to the second VIEW Controller created.
Below is the part of the code I wrote:
#IBAction func touchID(_ sender: Any){
let context:LAContext = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
{
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Autorization Required", reply: { (wasSuccessful, error) in
if wasSuccessful {
print("Correct")
//let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewControllerID") as! SecondViewController
//self.present(vc, animated: true, completion: nil)
}
else
{
print("Incorrect")
}
})
}else{
print("TouchID/Facec ID not configured")
}
}
}
I am a beginner.
Try to do this:
Your "button" to proceed must call to your TouchID function. When you print "correct", you must create the navigation by code. If you print "Incorrect", dont create the navigation.
If you print "TouchID/Face ID not configured", you should show an alertview and maybe open the app settings configuration to enable/disable touch/faceId.
Related
I am trying to implement a WKLongPressGestureRecognizer in my app. The long press gesture is recognized and the alert it presented. However, when I select the option to clear the table (I'm using Realm), the alert controller is dismissed, but the take is not cleared. When I tried to debug by adding breakpoints in the code, it seemed that the code inside the closure was being skipped completely. Any idea what I'm missing? (Should I be using an action sheet sided of an alert sheet?) I've tried so many different things. Here's the code with the print statements that I've been using to debug. None of the print statements inside the closures are currently being triggered.
#IBAction func handleLongPress(_ sender: Any) {
print("long press pressed")
WKInterfaceDevice.current().play(.click)
let clearAction = WKAlertAction(title: "Clear", style: .destructive) {
print("clear button pressed")
}
let cancelAction = WKAlertAction(title: "Cancel", style: .default) {
print("cancel button pressed")
}
presentAlert(withTitle: "Are you sure?", message: "Action cannot be undone", preferredStyle: .alert, actions: [clearAction, cancelAction])
print("exiting long press")}
Thanks for any input or advice.
The gesture recognizer will call selector handleLongPress twice, once with state == began, and once with state cancelled. I found that this causes the problems with presentAlert that you're describing.
Try checking for the began state:
#IBAction func handleLongPress(_ sender: WKGestureRecognizer) {
guard sender.state == .began else {
return
}
// rest of handleLongPress implementation goes here
}
This should ensure your alert is presented exactly once.
i have a simple chat view that contain table view, text field and a send button
i'm using IQKeyboardManager to handle keyboard appearance but the unexpected behavior is that when i click on send button the keyboard disappear that behavior don't happen on chat apps like whatsApp the keyboard remain appear'
how to handle this behavior to be like whatsApp
Update : here is the send button code
#IBAction func sendPressed(_ sender: AnyObject) {
//TODO: Send the message to Firebase and save it in our database
if (messageTextfield.text?.isEmpty)!{
showAlert(alertTitle: "", alertMessage: "can't send empty Message", actionTiltle: "Ok")
}
else{
messageTextfield.isEnabled=false
sendButton.isEnabled=false
let messageDB=Database.database().reference().child("Messages")
let dictionary:[String:String]=["Sender":(Auth.auth().currentUser?.email)!,"MessageBody":messageTextfield.text!]
messageDB.childByAutoId().setValue(dictionary)
messageTextfield.text=""
messageTextfield.isEnabled=true
sendButton.isEnabled=true
}
}
The issue is with isEnable property used isUserInteration property instead of that
#IBAction func sendPressed(_ sender: AnyObject) {
//TODO: Send the message to Firebase and save it in our database
if (messageTextfield.text?.isEmpty)!{
showAlert(alertTitle: "", alertMessage: "can't send empty Message", actionTiltle: "Ok")
}
else{
messageTextfield.isUserInteractionEnabled=false
sendButton.isUserInteractionEnabled=false
let messageDB=Database.database().reference().child("Messages")
let dictionary:[String:String]=["Sender":(Auth.auth().currentUser?.email)!,"MessageBody":messageTextfield.text!]
messageDB.childByAutoId().setValue(dictionary)
messageTextfield.text=""
messageTextfield.isUserInteractionEnabled=true
sendButton.isUserInteractionEnabled=true
}
}
I have a window with some NSTextFields. When I click in one and edit the value and press return, I want the focus to go back to what it was before. I don't want the blue ring around the text field and I don't want further keystrokes going to that text field. I would have thought this would happen automatically.
I tried these, and none of them work
sender.resignFirstResponder()
sender.window?.makeFirstResponder(nil)
InspectorWindowController.window?.makeFirstResponder(nil)
AnotherWindowController.window?.becomeFirstResponder()
I'm doing these at the end of my IBAction associated with the text field. Maybe I have to do it from somewhere else?
Thanks
I figured this out. I guess the sent action is happening on another thread. So you have to call makeFirstResponder using Dispatch async.
DispatchQueue.main.async { //omg
sender.window?.makeFirstResponder(nil)
}
I needed to dismiss first responder in my SwiftUI macOS app and here what I found working in a way I need:
func controlTextDidEndEditing(_ obj: Notification) {
DispatchQueue.main.async {
guard let window = self.textField.window else {
return
}
// https://stackoverflow.com/questions/5999148/how-to-determine-whether-an-nssearchfield-nstextfield-has-input-focus
// We need to make sure that our text field is still first responder.
guard let textView = window.firstResponder as? NSTextView,
textView.delegate === self.textField else {
return
}
window.makeFirstResponder(nil)
}
}
I'm able to create pin's with their popup's as well as the info-light button to the right. And when tapped, the info button pops up an alert with some text and an "ok" button at the bottom. Is it now possible to add a link at the bottom of the text that will direct the user to a URL, saw wikkipedia for example in safari?
I've used the following called function to succesfully do so with a regular button in the mapview, but I can't figure out how to implement it the way I've stated above
#IBAction func webLink(sender: AnyObject) {
if let url = NSURL(string: "http://www.wikipedia.com") {
UIApplication.sharedApplication().openURL(url)
}
}
I have two view controllers and in ViewController 1 I have a date picker and a text field. I have another ViewController that displays what you have entered in that date picker and text field. I have a ¨change¨ button that returns you to that first view controller and a ¨doen¨button that show view controller 2. As of now the text in the textfield and the date that the user has selected, is gone every time you close the app or click the ¨change button(Go back to View controller 1).
How can i keep the text in the text field, and the selected date in the date picker.
I want the user to be able to change the information that has already been added.
Please add code.
http://imgur.com/PDzJrNJ (Picture of the main storyboard)
The way you are showing the ViewControllers is not optimal. In the best case, when you show something new (ViewController2), then dismiss it, it should lead you back to the previous screen (ViewController1). It should not instantiate a new instance of ViewController1, as the segues in you case are doing.
Please note that depending on how you present the second view the solution would be different.
In case your segue from the first controller to the second is a "Show" segue, you can just add the following to your "Change" button:
#IBAction func changeButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
In case you have a navigation controller (I do not see such on the screenshot), then you can use
#IBAction func changeButtonPressed(sender: AnyObject) {
self.navigationController.popViewControllerAnimated(true)
}
What you could try doing is saving the data before exiting the view controller.
In the viewWillDisappear method, you could add
NSUserDefaults.standardUserDefaults().setObject(textField.text, forKey: "userText")
NSUserDefaults.standardUserDefaults().setObject(datePicker.date, forKey: "userDate")
Later, when you reopen the same view controller, in the viewDidLoad method, you could add
if NSUserDefaults.standardUserDefaults().objectForKey("userText") != nil {
textField.text = NSUserDefaults.standardUserDefaults().objectForKey("userText") as! String
}
if NSUserDefaults.standardUserDefaults().objectForKey("userDate") != nil {
datePicker.setDate(NSUserDefaults.standardUserDefaults().objectForKey("userDate") as! NSDate, animated: true)
}
I do not use date pickers very frequently, but I believe this is the correct code. Otherwise, it would be very similar. Hope this helps.