@IBAction func saveTapped(_ sender: UIBarButtonItem) { let firstName = firstNameTextField.text ?? "" let lastName = lastNameTextField.text ?? "" let birthdate = birthdatePicker.date let appDelegate = UIApplication.shared.delegate as! AppDelegate let context = appDelegate.persistentContainer.viewContext let newBirthday = Birthday(context: context) newBirthday.firstName = firstName newBirthday.lastName = lastName newBirthday.birthdate = birthdate as NSDate as Date newBirthday.birthdayId = UUID().uuidString // MARK: - === Save image
if (imageView.image) != nil { let vgoImage = imageView.image!.pngData() as NSData? newBirthday.fioImg = (vgoImage!) as Data } else { let img = UIImage(named: "Face.png") let vgoImage = img!.pngData() as NSData? newBirthday.fioImg = (vgoImage!) as Data } if let uniqueId = newBirthday.birthdayId { print("The birthdayId is \(uniqueId)") } do { try context.save() let message = "Wish \(firstName) \(lastName) a Happy Birthday today!" let content = UNMutableNotificationContent() content.body = message content.sound = UNNotificationSound.default var dateComponents = Calendar.current.dateComponents([.month, .day], from: birthdate) dateComponents.hour = 8 let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) if let identifier = newBirthday.birthdayId { let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger) let center = UNUserNotificationCenter.current() center.add(request, withCompletionHandler: nil) } } catch let error { print("Could not save because of \(error).") } dismiss(animated: true, completion: nil) }