I've been trying to register a user to Firebase and while doing that I'm checking if there is an error. If error occurs, then my code block pops up a UIalertcontroller and prints "error" once So far it works fine.
When there is no error then it should do a segue and print "segue done". But While it has no error the code triggers segue twice and prints "segue done" twice. Why this happens?
#IBAction func registerButton(_ sender: Any) {
firestoreManager.registerUser(mail: mailField.text!, fullName: fullNameField.text!, username: usernameField.text!, password: passwordField.text!, instruments: instrumentArray, registerUserError: { [self] (canRegister) in
if canRegister == false {
print("registeruserfail")
let alert = UIAlertController(title: "Error", message: firestoreManager.errorString, preferredStyle: .alert)
self.present(alert, animated: false) {
alert.message = firestoreManager.errorString
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
print("error")
}
}
else { // this statement triggers twice
performSegue(withIdentifier: "toMainPage", sender: self)
print("segue done")
return
}
})
}
Related
Why is the completion not working ?
After the alert appears, its not transitioning to the other viewController
self.present(self.alertController2, animated: true, completion: {
self.performSegue(withIdentifier:"goToLogin", sender: self)
})
Self.present using for add this action into view . You need to add this code into your handler handler mean It means what will I do when you click OK
let alertbutton = UIAlertAction(title: "OK", style: .destructive, handler: {
(UIAlertAction) in
self.performSegue(withIdentifier: "your id", sender: self)
})
Complete example :
let alert = UIAlertController(title: "Its empty", message: "Going another view", preferredStyle: .alert)
let alertaction = UIAlertAction(title: "OK", style: .destructive, handler: { (UIAlertAction) in
self.performSegue(withIdentifier: "your segue id", sender: self)
})
alert.addAction(alertaction)
self.present(alert, animated: true, completion: nil)
You should write code first move into new view code run then show alert in main thread for displaying alert.
DispatchQueue.main.async {
// AlertView.showAlert(title: Messages.Network.title, message: Messages.Network.message)
// }
Im trying to check if a textFieldis empty, and if it is a popup should show:
func missingText (title: String, message: String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
#IBAction func registerButton(_ sender: UIButton) {
//Display errormsg for missing entry
if nameTextField.text!.isEmpty
{
missingText(title: "Error", message: "Missing name")
}
}
Nothing is happening when the textfield is empty. I (think) I have the exact same code in another app, and that's working fine.. What am I missing?
The nameTextFieldis hooked up.
I had a similar problem once, It turned out that instead of using the placeholder for description for the textfield, I used the Text. Both are located in Attributes inspector for the textField. So, if u use the Textfor description, the textField will never be empty.
I guess textfield text is nil. so that it may not be calling. try with below code
#IBAction func registerButton(_ sender: UIButton) {
guard let text = textField.text, text != "" else{
missingText(title: "Error", message: "Missing name")
return
}
// do stuff with text,
}
u can try this one this might will help you . nametextfield.text , !text.empty
I am trying to show a UIAlertController which will display my terms of service. I can not simply paste the terms of service into the message body, what would be the best way to achieve this. I am also only calling it once on apps first launch.
func agree(){
let alert = UIAlertController(title: "Terms of Service", message:"", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Agree", style: .default, handler: { (action: UIAlertAction) in
print("OK")
}))
self.present(alert, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.bool(forKey: "Walkthrough") {
print("already shown")
// Terms have been accepted, proceed as normal
} else {
agree()
UserDefaults.standard.set(true, forKey: "Walkthrough")
}
}
Im having a problem where after I broadcast my screen live, I then try to record the screen but it doesn't work. Same thing happens when I record my screen and then I try to broadcast the screen live. Im not trying to do them together btw. This is after one is done and I try to use the other one. Let me know if you need to see code or more info. Im in Swift 3 and using the new replay kit framework. Thanks!
EDIT: THIS IS THE CODE IM USING
//LIVE STREAM REPLAYKIT=====================================================================
func broadcastActivityViewController(_ broadcastAVC: RPBroadcastActivityViewController, didFinishWith broadcastController: RPBroadcastController?, error: Error?) {
print("=====hello delegate \(broadcastController?.broadcastURL) (error)")
self.broadcastController = broadcastController
self.broadcastController?.delegate = self
broadcastAVC.dismiss(animated: true) {
self.broadcastController?.startBroadcast(handler: { error in
print("start broadcast \(error)")
print("\(broadcastController?.broadcastExtensionBundleID)")
print("==url=\(broadcastController?.broadcastURL)")
print("==serviceInfo=\(broadcastController?.serviceInfo)")
//This is called when the broadcast is live
})
}
}
func broadcastController(_ broadcastController: RPBroadcastController, didFinishWithError error: Error?) {
print("broadcastController====delegate")
let alert = UIAlertController(title: "Alert", message: "There was an error broadcasting your screen. Please try again", preferredStyle: UIAlertControllerStyle.alert)
// show the alert
self.view!.window?.rootViewController!.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.destructive, handler: { action in
// add action
}))
}
func broadcastController(_ broadcastController: RPBroadcastController, didUpdateServiceInfo serviceInfo: [String : NSCoding & NSObjectProtocol]) {
print("broadcastController====didUpdateServiceInfo")
}
//LIVE STREAM REPLAYKIT=========================================================
//RECORD SCREEN REPLAYKIT-------------------------------------------------------------------
func startRecoding() {
let recorder = RPScreenRecorder.shared()
if recorder.isAvailable {
recorder.startRecording(handler: { (error) in
if error == nil { // Recording has started
} else {
// Handle error
print("Dont Allow Recording")
}
})
} else {
print("Did not record screen")
//if iphone or ipad doesnt support replaykit
// create the alert
let alert = UIAlertController(title: "Alert", message: "Please make sure your device supports ReplayKit!", preferredStyle: UIAlertControllerStyle.alert)
// show the alert
self.view!.window?.rootViewController!.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Try Again!", style: UIAlertActionStyle.destructive, handler: { action in
// add action
}))
}
}
func stopRecording() {
let sharedRecorder = RPScreenRecorder.shared()
sharedRecorder.stopRecording(handler: { (previewViewController: RPPreviewViewController?, error) in
if previewViewController != nil {
print("stopped recording")
previewViewController!.previewControllerDelegate = self
let alertController = UIAlertController(title: "Recording", message: "Tap view to watch, edit, share, or save your screen recording!", preferredStyle: .alert)
let viewAction = UIAlertAction(title: "View", style: .default, handler: { (action: UIAlertAction) -> Void in
self.view?.window?.rootViewController?.present(previewViewController!, animated: true, completion: nil)
})
alertController.addAction(viewAction)
self.previewViewController = previewViewController!
self.previewViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
self.view?.window?.rootViewController!.present(alertController, animated: true, completion: nil)
}
else {
print("recording stopped working")
//create the alert================================
let alert = UIAlertController(title: "Alert", message: "Sorry, there was an error recording your screen. Please Try Again!", preferredStyle: UIAlertControllerStyle.alert)
// show the alert
self.view!.window?.rootViewController!.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Try Again!", style: UIAlertActionStyle.destructive, handler: { action in
// add action
}))
}
})
}
func previewControllerDidFinish(_ previewViewController: RPPreviewViewController) {
print("cancel and save button pressed")
previewViewController.dismiss(animated: true, completion: nil)
//dismiss preview view controller when save or cancel button pressed
}
I believe that this is a bug in ReplayKit, I'm not sure if it has been resolved as of 10.1 or not, but it is worth trying the 10.1 beta to see if it solves your issue.
Whenever I press "Cancel" then "Delete Draft", the mail composer won't be dismissed. The error I'm getting is "Thread 1: EXC_BAD_ACCESS (code=1, address=0x40363380)"
In my TableViewController I have:
#IBAction func mailButton(sender: AnyObject) {
let emailComposer = EmailComposer()
if email != "" {
print(email)
if emailComposer.canSendMail() {
emailComposer.setRecipient(email)
let configuredMailComposeViewController = emailComposer.configuredMailComposeViewController()
presentViewController(configuredMailComposeViewController, animated: true, completion: nil)
}
} else {
let alertController = UIAlertController(title: "Sorry!", message: "No email found for this contact", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
//do nothing
}))
self.presentViewController(alertController, animated: true, completion:nil)
}
}
For those who don't know, EXC_BAD_ACCESS means its trying to access something in memory that is no longer there. I wrongfully created the EmailComposer() object after the button tap so it was going out of scope. So this:
let emailComposer = EmailComposer()
...should have been created here, for example:
class TableViewController: UITableViewController {
let emailComposer = EmailComposer()