'Location based notifications Swift

I am new to Swift. I'm trying to create a user notification app based on the location the user chooses. I keep finding old versions here in Stack Overflow and the newer ones that I tried don't work. Geocoding works perfectly fine, but trying to receive a notification by changing the coordinates in the simulator or using my current coordinates to use my actual device, exiting and entering the radius and I can't get a notification on both.

Here are the things that I have done:

  • I've imported CoreLocation
  • Info Plist: I've called Privacy - Location Always Usage Description and Privacy - Location When In Use Usage Description with description text.

If someone can let me know what I'm doing wrong here please do.

Here is the code that I've been using to test a notification with a specific location.

import UIKit
import UserNotifications
import CoreLocation

class NotificationViewController: UIViewController,CLLocationManagerDelegate {
    
    let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager.delegate = self
        requestNotification()
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        
        
    }
    
    func requestNotification(){
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge, .sound]) { granted, error in
            if granted{
                print("Granted access to send notifications")
            }else{
                if error != nil{
                    print("An error occurred accessing user notifications")
                }
            }
        }
        
    }
    
    
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        localNotification()
    }
    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        localNotification()
    }
    
    
    func localNotification(){
        
        let content = UNMutableNotificationContent()
        content.title = "Reminder Title"
        content.body = "Reminder Body"
        content.sound = .default
        
        
        let center = CLLocationCoordinate2D(latitude: 37.335400, longitude: -122.009201)
        let region = CLCircularRegion(center: center, radius: 10, identifier: "Headquarters")
        region.notifyOnEntry = true
        region.notifyOnExit = false
        let trigger = UNLocationNotificationTrigger(region: region, repeats: false)
        
        let notificationRequest = UNNotificationRequest(identifier: "region", content: content, trigger: trigger)
        
        UNUserNotificationCenter.current().add(notificationRequest) { error in
            if error != nil{
                print("Error")
            }else{
                print("Added")
            }
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source