Post

Replies

Boosts

Views

Activity

How do I display a value in a label (the value keeps changing/updating)
How do I display a value in a label in my Xcode app (the value keeps changing/updating)? So in my code, there is a pinned location and a user location, and I made some code to make a variable that tells the distance between the two points that continuously updates. I want to display this variable in a label so I can see how it updates. Here is my code:                 let value = userLocation.distance(from: pointLocation)
7
0
3.1k
Jun ’21
How to save a photo to the camera roll Xcode?
In my project, the user should be able to take a photo, and save it to the camera roll. How do I add this functionality to my code. I have already added the save photo button, and maybe I call a function to save it. Someone please assist me in doing this thanks. My code down below: // //  ViewController.swift //  takePictureWithCamera // //  Created by Vaibhav Satishkumar on 11/20/21. // import UIKit class ViewController: UIViewController {          @IBAction func savePhoto(_ sender: Any) {               }          @IBOutlet var imageView: UIImageView!     @IBOutlet var button: UIButton!                        override func viewDidLoad() {         super.viewDidLoad()         imageView.backgroundColor = .secondarySystemBackground                                         }          @IBAction func didTapButton(){         let picker = UIImagePickerController()         picker.sourceType = .camera         picker.allowsEditing = true         picker.delegate = self         present(picker, animated: true)                       }           } extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{          func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {         picker.dismiss(animated: true, completion: nil)              }     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {         picker.dismiss(animated: true, completion: nil)         guard let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else         {             return         }                  imageView.image = image                       } }
1
0
1.5k
Nov ’21
How to use userDefaults with multiple view controllers?
In my code, a user enters their address, and gets the coordinates of that address in return. These coordinates should get saved to user defaults. Then, these coordinates are accessed by another view, and a mapView with a pin of the user's address is shown. ViewController 1     @IBOutlet var geocodeButton: UIButton!     @IBAction func geocode(_ sender: UIButton) {         guard let country = countryTextField.text else { return }         guard let street = streetTextField.text else { return }         guard let city = cityTextField.text else { return }         // Create Address String         let address = "\(country), \(city), \(street)"         // Geocode Address String         geocoder.geocodeAddressString(address) { (placemarks, error) in             // Process Response             self.processResponse(withPlacemarks: placemarks, error: error)         }         // Update View         geocodeButton.isHidden = true         activityIndicatorView.startAnimating()     }     private func processResponse(withPlacemarks placemarks: [CLPlacemark]?, error: Error?) {         // Update View         geocodeButton.isHidden = false         activityIndicatorView.stopAnimating()         if let error = error {             print("Unable to Forward Geocode Address (\(error))")             locationLabel.text = "Unable to Find Location for Address"         } else {             var location: CLLocation?             if let placemarks = placemarks, placemarks.count > 0 {                 location = placemarks.first?.location             }             if let location = location {                 let coordinate = location.coordinate                 locationLabel.text = "\(coordinate.latitude), \(coordinate.longitude)"                 UserDefaults.setValue(coordinate.latitude, forKey: "lat")                 UserDefaults.setValue(coordinate.longitude, forKey: "long")             } else {                 locationLabel.text = "No Matching Location Found"             }         }     } extension String {     var doubleValue: Double {         return Double(self) ?? 0     } } View Controller 2: override func viewDidLoad() {         super.viewDidLoad()                  if let latitude = UserDefaults.value(forKey: "lat") as? Double{             coordinate1.latitude = lat         }         if let longitude = UserDefaults.value(forKey: "lat") as? Double{             coordinate1.longitude = long         } When I try to run this, I get an error: Thread 1: "[<NSUserDefaults 0x1dad81bd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key lat." Please help me solve this issue soon, thanks!
1
0
1.3k
Dec ’21
Why isn't my local notification getting delivered in the background?
My app is supposed to deliver a local notification in the background, but it isnt. The notification works when the app is in the foreground but it just fails to work otherwise. Please help me. Here is my code: import UIKit import CoreLocation import UserNotifications class ViewController1: UIViewController, CLLocationManagerDelegate, UNUserNotificationCenterDelegate {     override func viewDidLoad() {         super.viewDidLoad() UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {             (success, error) in             if error != nil {                 print("Authorization failed")             }         }     }              func sendNotification(inSeconds: TimeInterval, completion: @escaping (_ Success: Bool)->()){         let trigger = UNTimeIntervalNotificationTrigger(timeInterval: inSeconds, repeats: false)         let content = UNMutableNotificationContent()         content.title = "Important question:"         content.body = "Have you locked your door?"         content.sound = .defaultCriticalSound(withAudioVolume: 20)                           let request = UNNotificationRequest(identifier: "customNotification", content: content, trigger: trigger)         UNUserNotificationCenter.current().add(request) { (error) in             if error != nil{                 completion(false)             }else{                 completion(true)             }         }     } var isAtHome = 0                     while value < 100 && isAtHome == 0{                 deliverNotification()                 isAtHome = 1             }             while value > 100 && isAtHome == 1{                 isAtHome = 0             }             return value         }     } }
1
1
624
Jan ’22