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.
Related
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
}
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.
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)
}
}
My photo app crashes when user selects video. The user can select photo and it works fine, but I'd like to remove the option to take a video. How do I disable video? The relevant chunk of code is below...
#IBAction func choosePhoto(_ sender: UIButton) {
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
imagePicker = UIImagePickerController()
imagePicker.delegate = self
if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
let cameraAction = UIAlertAction(title: "Use Camera", style: .default) { (action) in
let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
if (status == .authorized) {
self.displayPicker(type: .camera)
}
if (status == .restricted) {
self.handleRestricted()
}
if (status == .denied) {
self.handleDenied()
}
if (status == .notDetermined) {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted) in
if (granted) {
self.displayPicker(type: .camera)
}
})
}
}
alertController.addAction(cameraAction)
}
if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
let photoLibraryAction = UIAlertAction(title: "Use Photo Library", style: .default) { (action) in
let status = PHPhotoLibrary.authorizationStatus()
if (status == .authorized) {
self.displayPicker(type: .photoLibrary)
}
if (status == .restricted) {
self.handleRestricted()
}
if (status == .denied) {
self.handleDenied()
}
if (status == .notDetermined) {
PHPhotoLibrary.requestAuthorization({ (status) in
if (status == PHAuthorizationStatus.authorized) {
self.displayPicker(type: .photoLibrary)
}
})
}
}
alertController.addAction(photoLibraryAction)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
func handleDenied() {
let alertController = UIAlertController(title: "Camera Access Denied", message: "This app does not have acces to your device's camera. Please update your settings.", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Go To Settings", style: .default) { (action) in
DispatchQueue.main.async {
UIApplication.shared.open(NSURL(string: UIApplicationOpenSettingsURLString)! as URL)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.popoverPresentationController?.sourceView = self.photoImage
alertController.popoverPresentationController?.sourceRect = self.photoImage.bounds
alertController.addAction(settingsAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
func handleRestricted() {
let alertController = UIAlertController(title: "Camera Access Denied", message: "This device is restricted from accessing the camera at this time", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.popoverPresentationController?.sourceView = self.photoImage
alertController.popoverPresentationController?.sourceRect = self.photoImage.bounds
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
func displayPicker(type: UIImagePickerControllerSourceType) {
self.imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: type)!
self.imagePicker.sourceType = type
self.imagePicker.allowsEditing = true
DispatchQueue.main.async {
self.present(self.imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
photoImage.contentMode = .scaleAspectFill
photoImage.image = chosenImage
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
Put this line right after imagePicker.delegate = self
imagePicker.mediaTypes = [kUTTypeImage as String]
Remember to add import MobileCoreServices at the beginning of the file.
What you want to do is after you init imagePicker set the following property
imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
https://developer.apple.com/documentation/uikit/uiimagepickercontroller/1619173-mediatypes
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 !