Here is my code:
let library = ALAssetsLibrary()
library.writeVideoAtPathToSavedPhotosAlbum(NSURL(string: path), completionBlock: { (url, error) -> Void in
if error != nil {
let error = UIAlertController(title: "Error!", message:
"Your video is NOT saved!", preferredStyle: UIAlertControllerStyle.Alert)
error.addAction(UIAlertAction(title: "Shit!", style: UIAlertActionStyle.Default,handler:
{ action in navigationController!.popViewControllerAnimated(true) }))
self.presentViewController(error, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: "Done!", message:
"Your video is saved!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Nice!", style: UIAlertActionStyle.Default,handler:
{ action in navigationController!.popViewControllerAnimated(true) }))
self.presentViewController(alertController, animated: true, completion: nil)
}
})
And the AlertController that says that video is saved appears twice if I press "OK" really fast (If I wait a few seconds everything is fine). What's the problem and how can I solve it ?
Related
I have a problem trying to put an alert message after completing the action. The application crashes.
#IBAction func deleteAccountAction(_ sender: Any) {
let userID = prefs.value(forKey: "userId") as! String
print("user id: \(userID)")
let alert = UIAlertController(title: "Delete account", message: "Are you sure you want to delete your account?, This action cannot be reversed.", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// ...
}
let okayAction = UIAlertAction(title: "OK", style: .default) { (action) in
RestAPIManager.sharedInstance.deleteAccount(userID: userID){
(json, error) in
if(json != JSON.null){
print(json)
if(json["success"] == true){
//here i want succes alert
}else{
self.errorAlert()
}
}else{
}
}
}
alert.addAction(okayAction)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
func errorAlert(){
var dialogMessage = UIAlertController(title: "Error", message: "Error", preferredStyle: .alert)
self.present(dialogMessage, animated: true, completion: nil)
}
I tried to put an alert message after the action but I can't.
is solved needs to be async.
DispatchQueue.main.async {
self.errorAlert()
}
I have just coded a signup page in which if user does not enter text in any of the uitextfield then it should receive an error message (UIAlert) ; else (if user enter text in all uitextfield) signup page will present for Firebase Authentication.
in my code, user will direct to sign-in page only if authentication successfully complete else it should remain in signup page with alert message.
Problem - my code is able to produce alert message but same time it is taking user to sign-in page automatically even when there is an error. which means it is performing a segue that takes user to sign-in page i.e. segue unwinding irrespective of alert message.
can anyone help me why this may be happening?
#IBAction func registerPressed(_ sender: Any) {
if nameText.text!.isEmpty || genderText.text!.isEmpty || countryText.text!.isEmpty || yourSchool.text!.isEmpty || yourClass.text!.isEmpty {
print("Please fill all fields") //my code is printing this error
//alert message popup - my code is ble to produce this alert but same it is performing segue and taking user to signin page
//ideally, i want user to be in signup page unless all criteria meet
let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in
print("Okay")
}))
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)
}
else {
Auth.auth().createUser(withEmail: yourEmail.text!, password: yourPassword.text!) { (user, error) in
if error != nil {
///print errror message
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in
print("Okay")
}))
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1;
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)
}
else {
print("You have successfully signed up")
self.performSegue(withIdentifier: "JoinUs2SignPage", sender: self)
//updating user information
let userID = Auth.auth().currentUser!.uid
let usertype: String = "Student"
self.ref.child("users").child(userID).setValue(["usertype": usertype ,"username": self.nameText.text!, "usergender": self.genderText.text!, "usercountry": self.countryText.text!, "userschool": self.yourSchool.text!, "userclass": self.yourClass.text!,])
}
}
}
}
From this bit of code it doesn't really look like anything is wrong. However, you should block the UI while Auth.auth().createUser(...) is running. Otherwise there's a chance that you call registerPressed with everything correct, but then delete the text from a label and call it again before the callback. That way you have an alert and then the segue is called.
You're also doing something quite crazy with the way that you're presenting your alerts. Instead of creating a new window, adding to it a view controller and all that jazz, just call self.present(alertController, animated: true). E.g.
let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in
print("Okay")
}))
self.present(alertController, animated: true)
Remove the segue and push to destination view controller programmatically.
self.navigationController?.pushViewController(destinationVC, animated: true)
I was doing a silly mistake where I created the segue directly on IBAction and hence whenever I was pressing the button it was performing the segue irrespective UIAlerts. my updated code is below :
#IBAction func registerPressed(_ sender: Any) {
if nameText.text!.isEmpty || genderText.text!.isEmpty || countryText.text!.isEmpty || yourSchool.text!.isEmpty || yourClass.text!.isEmpty {
//alert message popup
let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in
print("Okay")
}))
self.present(alertController, animated: true, completion: nil)
}
else {
Auth.auth().createUser(withEmail: yourEmail.text!, password: yourPassword.text!) { (user, error) in
if error != nil {
///print errror message
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in
print("Okay")
}))
self.present(alertController, animated: true, completion: nil)
}
else {
let alertController = UIAlertController(title: "Congratulation", message: "You have successfully signed up", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Get Started", style: .default, handler: { (action:UIAlertAction) in
self.performSegue(withIdentifier: "back2SignPage", sender: self)
}))
self.present(alertController, animated: true, completion: nil)
//updating user information
let userID = Auth.auth().currentUser!.uid
let usertype: String = "Student"
self.ref.child("users").child(userID).setValue(["usertype": usertype ,"username": self.nameText.text!, "usergender": self.genderText.text!, "usercountry": self.countryText.text!, "userschool": self.yourSchool.text!, "userclass": self.yourClass.text!,])
}
}
}
}
I am having issues with two segues however, since I used the same logic in the implementation of each of these segues I'm going to go ahead and condense everything into one question. Basically, I am trying to initialize a segue after performing createUser function with firebase and after performing login function also handled by Firebase, however, when the SignUp and Login buttons are clicked there seems to be no response to the segue being called. Below is the code for each of the #IBActions
#IBAction func signUpComplete(_ sender: Any) {
FIRAuth.auth()?.createUser(withEmail: signUpEmailField.text!, password: signUpPasswordField.text!, completion: { (user: FIRUser?, error) in
if self.signUpEmailField.text == "" || self.signUpPasswordField.text == "" {
let alert = UIAlertController(title: "Oops!", message: "A valid E-mail and Password are Required", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
if FIRAuthErrorCode.errorCodeEmailAlreadyInUse != nil {
let emailExists = UIAlertController(title: "Oops!", message: "A user with this E-Mail already exists!", preferredStyle: UIAlertControllerStyle.alert)
emailExists.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(emailExists, animated: true, completion: nil)
}
else {
DispatchQueue.main.async () {
self.performSegue(withIdentifier: "signUpSucceededSegue", sender: self)
}
}
}
})
}
and
#IBAction func loginButton(_ sender: Any) {
FIRAuth.auth()?.signIn(withEmail: emailLoginField.text!, password: passwordLoginField.text!, completion: { (user: FIRUser?, error) in
if self.emailLoginField.text == "" || self.passwordLoginField.text == ""{
let alert = UIAlertController(title: "Alert", message: "E-Mail is Required", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
if FIRAuthErrorCode.errorCodeUserNotFound != nil {
let invalidLogin = UIAlertController(title: "Alert", message: "E-Mail/Password Incorrect", preferredStyle: UIAlertControllerStyle.alert)
invalidLogin.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(invalidLogin, animated: true, completion: nil)
}
else{
DispatchQueue.main.async () {
self.performSegue(withIdentifier: "loginSucceededSegue", sender: self)
}
}
}
})
are there any glaring logical errors at first glance or is there a better way to implement a segue into these functions?
Embed a Navigation Controller to the very first view of your storyboard
Then Try using this code:-
#IBAction func signUpComplete(_ sender: Any) {
if self.signUpEmailField.text == "" || self.signUpPasswordField.text == "" {
let alert = UIAlertController(title: "Oops!", message: "A valid E-mail and Password are Required", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}else{
FIRAuth.auth()?.createUser(withEmail: self.signUpEmailField!.text, password: self.signUpPasswordField!.text, completion: { (user, err) in
if let ErrorCode = FIRAuthErrorCode(rawValue: err!._code){
switch ErrorCode {
case .errorCodeEmailAlreadyInUse : let emailExists = UIAlertController(title: "Oops!", message: "A user with this E-Mail already exists!", preferredStyle: UIAlertControllerStyle.alert)
emailExists.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(emailExists, animated: true, completion: nil)
break
default : break
}
}else{
let nextView = self.navigationController!.storyboard!.instantiateViewController(withIdentifier: "signUpSucceededSegue")
self.navigationController!.pushViewController(nextView, animated: true)
}
})
}
}
Then alter your other code-block in the similar fashion.
So here is the deal. Right now I have a viewcontroller where the user can signup, and it is working (checked in Firebase console). The next step is that I have another view with two fields and a Log in button. That login button now has a segue to the third view, home. But it is possible to click it even though nothing has been entered in the two textfields. The button should only work if the user enters his or he details he made in the signup view, thus logging in.
How can I do this?
already have the login code:
#IBAction func loginAction(sender: AnyObject)
{
if self.emailField.text == "" || self.passwordField.text == ""
{
let alertController = UIAlertController(title: "Oops!", message: "Please enter an email and password.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
else
{
FIRAuth.auth()?.signInWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in
if error == nil
{
self.emailField.text = ""
self.passwordField.text = ""
}
else
{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
In the second if statement you see that it clears the fields to show the login worked.
What i need is that users can only login if the correct details are filled in, that are in the Firebase database.
Instead of checking if there isnt any errors, check that there is data is the user object like this.
FIRAuth.auth()?.signInWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in
if user != nil
{
let VC = storyboard?.instantiateViewControllerWithIdentifier("Identifier") as! AccountViewController //The file that controls the view
self.presentViewController(VC, animated: true, completion: nil)
}
else
{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
I managed to fix it by adding self. infront of
storyboard?.instantiate.
Thus creating:
self.storyboard?instantiate.
How to access the view controller which is evoked by the stopRecording() method of ReplayKit framework. And how to save the video in the camera roll?
Try this if you haven't gotten to work yet:
func stopRecording() {
let sharedRecorder = RPScreenRecorder.sharedRecorder()
sharedRecorder.stopRecordingWithHandler { (previewViewController: RPPreviewViewController?, error: NSError?) 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?.presentViewController(previewViewController!, animated: true, completion: nil)
})
alertController.addAction(viewAction)
self.previewViewController = previewViewController!
self.previewViewController.modalPresentationStyle = UIModalPresentationStyle.FullScreen
self.view?.window?.rootViewController!.presentViewController(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!.presentViewController(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Try Again!", style: UIAlertActionStyle.Destructive, handler: { action in
// add action
}))
}
}
}
internal func previewControllerDidFinish(previewController: RPPreviewViewController) {
self.previewViewController.dismissViewControllerAnimated(true, completion: nil)
print("cancel and save button pressed")
}