Post

Replies

Boosts

Views

Activity

Reply to getting Error While using Date picker class ActionSheetDatePicker.init
@OOPer `//  SignupViewController.swift import UIKit import SwiftValidator class SignupViewController: BaseViewController {         @IBOutlet weak var dobTextField: UITextField!     var viewModel = SignUpViewModel()               override func viewDidLoad() {         super.viewDidLoad()     } } extension SignupViewController:UITextFieldDelegate { func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {          if textField == dobTextField {         self.view.endEditing(true)         super.datePickerTapped { (dateString) in             self.dobTextField.text = dateString         }         return false     }          return true } }` `// BaseViewController.swift import UIKit import NVActivityIndicatorView import Toast_Swift import ActionSheetPicker_3_0 import TweeTextField import SwiftValidator class BaseViewController: UIViewController {          func datePickerTapped(completionHandler: @escaping ((String)->Void)) {                  let cancelButton:UIButton =  UIButton(type: .custom)         cancelButton.setTitle("Cancel", for: .normal)         cancelButton.titleLabel?.font = crudFonts.appRegularFont         cancelButton.setTitleColor(UIColor.black, for: .normal)         cancelButton.frame = CGRect(x: 0, y: 0, width: 55, height: 25)                  let doneButton:UIButton =  UIButton(type: .custom)         doneButton.setTitle("Done", for: .normal)         doneButton.titleLabel?.font = crudFonts.appRegularFont         doneButton.setTitleColor(UIColor.black, for: .normal)         doneButton.frame = CGRect(x: 0, y: 0, width: 55, height: 25)                  let maxiumDate = Calendar.init(identifier: Calendar.Identifier.gregorian).date(byAdding: .year, value: -18, to: Date())         let picker = ActionSheetDatePicker.init(title: "Date of Birth", datePickerMode: .date, selectedDate: maxiumDate, doneBlock: { (picker, selectedIndex, selectedValue) in                          if let date = selectedIndex as? Date {                 let formatter = DateFormatter()                 formatter.dateFormat = "dd/MM/YYYY"                 completionHandler(formatter.string(from: date))             }         }, cancel: { (picker) in                      }, origin: Global.getTopMostViewController()?.view)         picker?.minimumDate = Calendar.init(identifier: Calendar.Identifier.gregorian).date(byAdding: .year, value: -100, to: Date())         picker?.maximumDate = maxiumDate         picker?.toolbarButtonsColor = .black         picker?.toolbarBackgroundColor = UIColor.white         picker?.setCancelButton(UIBarButtonItem(customView: cancelButton))         picker?.setDoneButton(UIBarButtonItem(customView: doneButton))                  picker?.show()                  if let datePicker = picker?.pickerView as? UIDatePicker {             if #available(iOS 13.4, *) {                 datePicker.preferredDatePickerStyle = UIDatePickerStyle.wheels             }         }     }      }`
Jun ’21
Reply to reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'
The issue is caused because the alert is being shown from the background thread as the completion handler in getNotificationSettings is being run in the background thread. To prevent this crash present alert in the main thread am done the following changes in my code. func checkNotificationAllowed(){ let data = defaults.object(forKey:"mute") as? Bool print(data!) if (data != nil) == true { let current = UNUserNotificationCenter.current() current.getNotificationSettings(completionHandler: { permission in switch permission.authorizationStatus { case .authorized: print("User granted permission for notification") case .denied: print("User denied notification permission") DispatchQueue.main.async {[weak self] in guard let weakSelf = self else {return} let alert = UIAlertController(title: "Turn On Notifications".localized(), message: "Notifications are disabled. Please turn on app notifications to get device alerts.".localized(), preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Don't Allow".localized(), style: .cancel, handler: { action in self.dismiss(animated: true, completion: nil) })) alert.addAction(UIAlertAction(title: "Allow".localized(), style: .destructive, handler: { action in weakSelf.dismiss(animated: true, completion: nil) })) weakSelf.present(alert, animated: true, completion: nil) } case .notDetermined: print("Notification permission haven't been asked yet") case .provisional: // @available(iOS 12.0, *) print("The application is authorized to post non-interruptive user notifications.") case .ephemeral: // @available(iOS 14.0, *) print("The application is temporarily authorized to post notifications. Only available to app clips.") @unknown default: print("Unknow Status") } }) } }
Mar ’22