makeAlert Function Returns Always False - swift

The function below always returns false. I tried to put return inside of the completion it did not accept either.
Can you please help me?
// MARK: - make Alert for user Input
func makeAlert(message: String, defaultButtonText: String, cancelButtonText: String) - > Bool {
var answer = Bool()
let alert = UIAlertController(title: "Warning", message: message, preferredStyle: .alert)
let actionYes = UIAlertAction(title: defaultButtonText, style: .default) {
(action) in
answer = true
}
let actionNo = UIAlertAction(title: cancelButtonText, style: .default) {
(action) in
answer = false
}
alert.addAction(actionNo)
alert.addAction(actionYes)
self.present(alert, animated: true, completion: {
print(answer)
})
return answer
}

You have to use completion like this.
func makeAlert(message: String,defaultButtonText: String, cancelButtonText: String, completion: #escaping ((Bool) -> Void)) {
let alert = UIAlertController(title: "Warning", message: message, preferredStyle: .alert)
let actionYes = UIAlertAction(title: defaultButtonText, style: .default) { (action) in
completion(true)
}
let actionNo = UIAlertAction(title: cancelButtonText, style: .default) { (action) in
completion(false)
}
alert.addAction(actionNo)
alert.addAction(actionYes)
self.present(alert, animated: true, completion: {
})
}
Usage :
makeAlert(message: "Test", defaultButtonText: "Test", cancelButtonText: "Test") { (action) in
if action {
// Do code for true part
} else {
// Do code for false part
}
}
EDIT
As per the commnet. How to use in FSCalendar
func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
makeAlert(message: "Test", defaultButtonText: "Yeah", cancelButtonText: "No") { (action) in
if action {
calendar.select(date)
}
}
return false
}

Related

How to create extension for UIAlertController in swift?

I am trying to modulraise my code I am able to create extension for alertController without target
extension UIViewController {
func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message:
message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {_ in
}))
self.present(alertController, animated: true, completion: nil)
}
}
but I am not sure how to create extension which have target and textfield(how to create and call that in viewcontroller)
my code for which i am trying to create extension
let alertController = UIAlertController(title: "enter name", message: nil, preferredStyle: .alert)
let addAction = UIAlertAction(title: "Ok", style: .default) {_ in
guard let name = alertController.textFields?.first?.text else { return }
let newTask = Task(name: name)
self.toDoStore?.add(newTask, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.insertRows(at: [indexPath], with: .automatic)
}
addAction.isEnabled = false
let cancelAction = UIAlertAction(title: CANCEL_ACTION, style: .cancel, handler: nil)
alertController.addTextField { textField in
textField.placeholder = "enter name.."
textField.addTarget(self, action: #selector(self.handleTextChanged), for: .editingChanged)
}
alertController.addAction(addAction);
alertController.addAction(cancelAction);
present(alertController, animated: true, completion: nil)
You could try the below extension that I'm using made a few changes according to your requirements.
extension UIViewController {
func showAlert(_ title: String?,
message: String?,
actions: [String: UIAlertAction.Style] = [:],
handler: ((UIAlertAction) -> Void)? = nil,
textFields: [((UITextField) -> Void)] = []) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
if actions.isEmpty {
alert.addAction(UIAlertAction(title: "Okay", style: .default))
} else {
for action in actions {
alert.addAction(UIAlertAction(title: action.key, style: action.value, handler: handler))
}
}
for textField in textFields {
alert.addTextField(configurationHandler: textField)
}
present(alert, animated: true)
}
}
A bit of advise you can always check how the parameters that you pass are accepted by the system API and customize your own function in a similar way.

AlertView Issue in iOS 13 Swift 4.2

Here is my generic class which I'm using everywhere in my project but it's making issues on iOS 13,
i.e AlertView appear n automatically disappear before any option selection.
//var status = false
class AlertClass{//This is shared class
static let sharedInstance = AlertClass()
//Show alert
func alertWindow(title: String, message: String, completion:#escaping (_ result: Bool) -> Void) {
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindow.Level.alert + 1
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction1 = UIAlertAction(title: "Yes", style: .destructive, handler: { action in
completion(true)
})
let defaultAction2 = UIAlertAction(title: "No", style: .destructive, handler: { action in
print("No")
completion(false)
})
alert2.addAction(defaultAction1)
alert2.addAction(defaultAction2)
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
}
func alertOkWindow(title: String, message: String, completion:#escaping (_ result: Bool) -> Void) {
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindow.Level.alert + 1
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction1 = UIAlertAction(title: "Ok", style: .default, handler: { action in
completion(true)
})
alert2.addAction(defaultAction1)
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
}
func alertDialogWithImage(msg: String, Icon : UIImage?) {
let alertDialogWithImage = UIWindow(frame: UIScreen.main.bounds)
alertDialogWithImage.rootViewController = UIViewController()
alertDialogWithImage.windowLevel = UIWindow.Level.alert + 1
let alrt = UIAlertController(title: " ", message: msg, preferredStyle: .alert)
let cancel = UIAlertAction(title: "Ok", style: .destructive) { (action) in
}
alrt.addAction(cancel)
let subview = (alrt.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
// subview.backgroundColor = UIColor(red: (145/255.0), green: (200/255.0), blue: (0/255.0), alpha: 1.0)
// alrt.view.tintColor = UIColor.black
alrt.view.tintColor = UIColor.red
let imageView = UIImageView(frame: CGRect(x: 120, y: 10, width: 35, height: 35))
imageView.image = Icon
alrt.view.addSubview(imageView)
alertDialogWithImage.makeKeyAndVisible()
alertDialogWithImage.rootViewController?.present(alrt, animated: true, completion: nil)
}
func alertWith3Actions(title: String, message: String, action1: String, action2: String, action3: String, completion:#escaping (_ result: Bool) -> Void) {
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindow.Level.alert + 1
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction1 = UIAlertAction(title: action1, style: .destructive, handler: { action in
completion(true)
})
let defaultAction2 = UIAlertAction(title: action2, style: .destructive, handler: { action in
print("No")
completion(false)
})
let defaultAction3 = UIAlertAction(title: action3, style: .destructive, handler: { action in
print("Later")
completion(false)
})
alert2.addAction(defaultAction1)
alert2.addAction(defaultAction2)
alert2.addAction(defaultAction3)
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
}
func alertTitleandMessage(title: String, message: String) {
DispatchQueue.main.async(execute: {
let alertTitleandMessage = UIWindow(frame: UIScreen.main.bounds)
alertTitleandMessage.rootViewController = UIViewController()
alertTitleandMessage.windowLevel = UIWindow.Level.alert + 1
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction2 = UIAlertAction(title: "OK", style: .destructive, handler: { action in
})
alert2.addAction(defaultAction2)
alertTitleandMessage.makeKeyAndVisible()
alertTitleandMessage.rootViewController?.present(alert2, animated: true, completion: nil)
})
}
}
I have change my code like I define these function in UIViewController Extension but after that I'm unable to call them in UITableViewCell & UICollectionViewCell.
Your help n guidance will be much appreciated.
Thanks in advance.
Br.

How can I add alert button till my data count? How can I save my data choose from a action index?

I'm present a alert when I click the button. I choose from a list (if how much data is available.) How can I save my data choose from a list index?
You can see UI in here
My AccountServices
class AccountServices {
static let databaseReference = Database.database().reference(withPath: "Accounts").child((Auth.auth().currentUser?.uid)!)
static var account = Account()
static func saveChanges() {
databaseReference.setValue(try! FirebaseEncoder().encode(AccountServices.account))
}
static func getAccount() {
databaseReference.observeSingleEvent(of: .value, andPreviousSiblingKeyWith: { (snapshot, _) in
account = try! FirebaseDecoder().decode(Account.self, from: snapshot.value!)
})
}
}
Variable
var product: ProductViewModel?
addButton Tapped
#IBAction func addToCartButtonTapped(_ sender: UIButton) {
let alert = UIAlertController(title: "Bu ürünü hangi sepetinize eklemek istersiniz ?", message: "", preferredStyle: .actionSheet)
var indexer = 0
for cart in AccountServices.account.cart! {
if cart.product == nil{
AccountServices.account.cart![indexer].product = [Product]()
}
let action = UIAlertAction(title: cart.name , style: .default, handler: { (sender) in
if let index = alert.actions.firstIndex(where: { $0 === sender }) {
AccountServices.account.cart?[index].product?.append(self.product) `//Error: Cannot convert value of type 'ProductViewModel?' to expected argument type 'Product'`
AccountServices.saveChanges()//TODO...
}
let addAlert = UIAlertController(title: "Sepetinize Eklendi.", message: "Ürününüz sepetinize eklendi.", preferredStyle: .alert)
let okButton = UIAlertAction(title: "Tamam", style: .default, handler: nil)
addAlert.addAction(okButton)
self.present(addAlert, animated: true, completion: nil)
})
alert.addAction(action)
indexer += 1
}
let cancelaction = UIAlertAction(title: "Vazgeç", style: .cancel, handler: nil)
alert.addAction(cancelaction)
present(alert, animated: true, completion: nil)
}
}

Firebase Phone Auth Swift

I would like to add the ability to register with the phone number while registering. I wrote a function but it doesn't work. Can you examine the code? What should I do?
func sendCode(_ sender: Any) {
let alert = UIAlertController(title: "Phone number", message: "Is this your phone number? \n \(emailField.text!)", preferredStyle: .alert)
let action = UIAlertAction(title: "Yes", style: .default) { (UIAlertAction) in
PhoneAuthProvider.provider().verifyPhoneNumber(self.emailField.text!, uiDelegate: nil) { (verificationID, error) in
if error != nil {
print("eror: \(String(describing: error?.localizedDescription))")
} else {
let defaults = UserDefaults.standard
defaults.set(verificationID, forKey: "authVID")
let navController = UINavigationController(rootViewController: CodeEntryView())
self.present(navController, animated: true, completion: {
})
}
}
}
let cancel = UIAlertAction(title: "No", style: .cancel, handler: nil)
alert.addAction(action)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
}

crop square image after get app gallary

I am new in swift. I made simple iphone app. Use this link: http://www.oodlestechnologies.com/blogs/Open-Image-Gallery-and-Take-photo-in-Swift and my app worked perfect. I want my app to select cropable image like facebook or telegram.
Like this:
My code:
private func showBottomActionSheet(){
let chooseOptionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let takePhotoAction = UIAlertAction(title: "Take Photo", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
self.openCamera()
})
let choosePhotoAction = UIAlertAction(title: "Choose Photo", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
self.openGallary()
})
let findImagesAction = UIAlertAction(title: "Find Images", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
print("Clicked Find Images")
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
(alert: UIAlertAction!) -> Void in
print("Cancelled")
})
chooseOptionMenu.addAction(takePhotoAction)
chooseOptionMenu.addAction(choosePhotoAction)
chooseOptionMenu.addAction(findImagesAction)
chooseOptionMenu.addAction(cancelAction)
self.presentViewController(chooseOptionMenu, animated: true, completion: nil)
}
private func openGallary()
{
self.picker!.allowsEditing = false
self.picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
presentViewController(picker!, animated: true, completion: nil)
}
private func openCamera()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
self.picker!.allowsEditing = false
self.picker!.sourceType = UIImagePickerControllerSourceType.Camera
self.picker!.cameraCaptureMode = .Photo
presentViewController(self.picker!, animated: true, completion: nil)
}else{
let alert = UIAlertController(title: "Camera Not Found", message: "This device has no Camera", preferredStyle: .Alert)
let ok = UIAlertAction(title: "OK", style:.Default, handler: nil)
alert.addAction(ok)
presentViewController(alert, animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
self.choosenPhoto = chosenImage
DefaultUser.setUserImage(chosenImage)
dismissViewControllerAnimated(true, completion: nil)
}
I found objective c code but didn't find code for swift 2.
Plz, advise me. thanks !