Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
CoreLocation & MVVM in SwiftUI: Build Robust, Real-Time Map
Rating: 4.5 out of 5(1 rating)
1,006 students

CoreLocation & MVVM in SwiftUI: Build Robust, Real-Time Map

Architect clean apps that understand where you are.
Created byNorbert Grover
Last updated 7/2025
English

What you'll learn

  • Build a complete SwiftUI application using the MVVM architecture.
  • Use CoreLocation to convert ZIP codes into geographic coordinates.
  • Integrate MapKit to perform local searches and display nearby points of interest.
  • Display search results dynamically in SwiftUI using List and ForEach.
  • Create custom data wrappers like IdentifiableMapItem to work with non-Identifiable types in SwiftUI.
  • Use @MainActor and @Published properties to manage state in real-time views.
  • Perform asynchronous searches with MKLocalSearch and Swift's async/await pattern.
  • Build reusable and decoupled services like a geolocation search service.
  • Understand how SwiftUI maps and views respond to data changes.
  • Deploy practical SwiftUI patterns to create scalable, real-world iOS applications.

Course content

4 sections23 lectures2h 45m total length
  • Location Tracker App Demonstration3:38

    In this video, we’ll walk through a simple demonstration of the Location Tracker app built with SwiftUI and CoreLocation. You’ll see how the app works from a user’s point of view and understand what the core features are.

    We’ll cover how the app:

    • Requests permission to access the user's location

    • Displays the current latitude and longitude on the screen

    • Updates automatically as the user’s location changes

    • Can be tested using the built-in location simulation in Xcode's iOS Simulator

    You’ll also learn how to simulate location updates using predefined routes like “Apple” or “City Run” in the simulator, which makes it easy to test without needing a real device.

    This video is ideal if you're just starting out and want to understand what the app does before diving into how it's built.

  • CLLocationManager: Core Methods and Callbacks0:59

    In this lecture, we’ll walk through the most important methods and delegate callbacks provided by CLLocationManager, the central class used to access location data in iOS.

    You'll learn how to request location permissions from users, start and stop location tracking, and request one-time updates. We’ll also explore how to check whether location services are enabled on a device—an essential step for robust apps.

    Equally important, we’ll explain how the delegate system works, and which methods you need to implement to receive location updates, handle errors, and respond to changes in user permission.

    By the end of this lecture, you’ll understand:

    • The difference between "always" and "when in use" location permissions

    • How to enable continuous or single-location updates

    • What each delegate method does and when it gets called

    • How to properly structure your location tracking logic using CoreLocation

  • Understanding CLLocationManager Methods and Callbacks
  • Understanding the LocationService File in SwiftUI2:21

    In this lesson, we break down the LocationService class—your app’s dedicated engine for managing all location-related tasks in a SwiftUI project. You'll learn how this class fits into the MVVM architecture by cleanly separating location tracking logic from your user interface.

    We walk through the key components of this class, including:

    • Importing and using CoreLocation and Combine

    • Initializing and configuring the location manager

    • Requesting permission to access the user's location

    • Starting and stopping location updates

    • Publishing live location data to other parts of your app

    A key part of this lesson is understanding what a delegate is and how CLLocationManager uses the delegate pattern to notify your class of new location updates or errors. We explain how this pattern works in practice, and how your class responds by implementing methods like didUpdateLocations and didFailWithError.

    By the end of this video, you'll have a solid grasp of how the LocationService class:

    • Requests location access from the user

    • Listens for and responds to GPS updates

    • Publishes location changes to your ViewModel

    • Integrates cleanly into a scalable SwiftUI architecture

    This lesson is essential for anyone looking to implement real-time location features while keeping their codebase clean and maintainable.

  • Mastering the LocationService in SwiftUI
  • Location Tracker ViewModel1:25

    Understanding `cancellables` and Subscriptions in the LocationViewModel

    =======================================================================

    In the LocationViewModel, `cancellables` is used to manage and clean up subscriptions to location updates using the Combine framework. To understand why this is necessary, it helps to first understand what subscriptions are.


    What Is a Subscription?

    -----------------------

    A subscription is a connection to a data source that can send updates over time. In Combine, this typically involves a Publisher (data source) and a Subscriber (listener).


    When you use `.sink` on a Publisher, you're telling the system:

    "Send me updates whenever the data changes."


    This connection continues until you cancel it, or the object managing it is destroyed. If you don't manage subscriptions properly, they can continue running and using memory or CPU unnecessarily, even if your app isn't using the data anymore.


    What Are Cancellables?

    ----------------------

    Combine provides a type called `Cancellable`, which represents an active subscription. When you no longer need updates, you can call `.cancel()` on a Cancellable to stop the data stream.


    Why Use a Set of Cancellables?

    ------------------------------

    In the ViewModel:

    ----------------------------------------

    private var cancellables = Set<AnyCancellable>()

    ----------------------------------------


    This line creates a collection that stores all active subscriptions. Combine will automatically cancel these when the ViewModel is deallocated, which ensures no leftover tasks are still running.


    How It Works in This Code

    --------------------------

    Example from the ViewModel:


    ----------------------------------------

    locationService.locationPublisher

        .receive(on: DispatchQueue.main)

        .sink { [weak self] location in

            self?.userLocation = location

        }

        .store(in: &cancellables)

    ----------------------------------------


    - `.sink` creates the subscription and listens to location updates.

    - `.receive(on: DispatchQueue.main)` makes sure updates are sent on the main thread (required for UI changes).

    - `.store(in: &cancellables)` keeps track of the subscription so Combine can cancel it when the ViewModel is destroyed.


    Why This Matters

    ----------------

    - Prevents memory leaks from subscriptions that are never canceled.

    - Saves battery and performance by stopping unused data streams.

    - Keeps your app clean, efficient, and easy to manage.


    Summary

    -------

    - A subscription is a connection to a live data stream.

    - A `Cancellable` lets you stop that connection when it's no longer needed.

    - A `Set<AnyCancellable>` stores and manages these subscriptions.

    - Combine automatically cancels stored subscriptions when the ViewModel is destroyed.


  • Understanding the LocationViewModel in MVVM (SwiftUI)1:06

    In this lesson, we explore the LocationViewModel—the key link between the location logic in your app and the SwiftUI user interface. Built within the MVVM (Model-View-ViewModel) architecture, this component helps keep your code clean, reactive, and well-organized.

    We’ll walk through how the LocationViewModel:

    • Subscribes to live updates from the LocationService

    • Publishes the current user location to the SwiftUI view

    • Requests location permissions

    • Starts and stops GPS tracking as needed

    You’ll learn about the tools that power this ViewModel, including:

    • @Published to trigger UI updates

    • ObservableObject to connect with SwiftUI’s data system

    • Combine’s .sink and AnyCancellable for managing subscriptions

    We also demonstrate how the ViewModel listens for updates on the main thread and performs clean-up when it’s no longer in use.

    By the end of this lesson, you’ll understand how the ViewModel:

    • Encapsulates your location-handling logic

    • Keeps your view layer simple and declarative

    • Supports testable, scalable app architecture

    This lesson is essential for understanding how to properly separate concerns and manage reactive location data in a SwiftUI app.

  • Understanding the ContentView in a SwiftUI Location App0:54

    In this lesson, we focus on the ContentView—the main user interface of the Location Tracker app. This view is responsible for presenting the user’s current location and reacting to updates from the ViewModel in real time.

    You'll learn how the view connects to the LocationViewModel using @StateObject, a key SwiftUI feature that ensures your view properly subscribes to and responds to changes in observable data.

    We’ll walk through:

    • The structure and logic of ContentView

    • How SwiftUI conditionally displays latitude and longitude

    • What happens when location data is loading or unavailable

    • How SwiftUI’s reactivity keeps the UI in sync with the latest location updates

    This lesson also reinforces the principles of the MVVM architecture:

    • The ViewModel handles location permissions and data logic

    • The view remains simple, declarative, and focused on UI

    By the end of this lesson, you'll understand how ContentView:

    • Subscribes to real-time updates using @StateObject

    • Displays location data cleanly and conditionally

    • Maintains a clean separation between UI and business logic

    This is a great example of how to build responsive, location-aware interfaces using SwiftUI and MVVM.

Requirements

  • Basic understanding of Swift programming language.
  • Familiarity with SwiftUI syntax and view composition.
  • Some experience building simple iOS apps in Xcode.
  • Comfort using Swift’s @State, @Binding, and @Published property wrappers.
  • Understanding of asynchronous code with async/await in Swift.
  • General knowledge of Model-View-ViewModel (MVVM) architecture.
  • Familiarity with the concept of protocols and structs in Swift.
  • Experience with navigation and layout in SwiftUI.
  • A Mac computer running macOS with the latest version of Xcode installed.
  • Access to a simulator or iOS device for testing MapKit functionality.

Description

Are you ready to build modern, scalable location-based apps with SwiftUI? This course will teach you how to harness the power of CoreLocation and MapKit, while applying the MVVM architecture pattern to create clean, maintainable, and testable code.

In this hands-on course, you’ll learn how to track user location in real time, display it on interactive maps, drop custom pins, request location permissions properly, and simulate location changes using Apple’s development tools. But we won’t stop at basic implementations — you’ll architect everything using the MVVM pattern, ensuring a clear separation of concerns between your view, business logic, and location services.

You’ll begin with the fundamentals of geolocation in iOS, then progressively build more advanced features such as live location tracking, map annotations, location simulation, and saving coordinates. Every lesson is grounded in real-world use cases, so you're building practical skills you can use in production.

By the end of the course, you’ll have created a fully functional MyLocationTracker app, structured to scale and designed with professional SwiftUI practices. Whether you're a SwiftUI beginner expanding into location features, or an experienced iOS developer refining your architecture, this course delivers practical, production-level skills.

Topics include:

  • CoreLocation fundamentals and permission handling

  • MapKit integration with SwiftUI

  • MVVM design pattern for location-based features

  • Real-time location tracking and annotation management

  • Simulating and testing location in Xcode’s iOS Simulator

Enroll today and learn how to build clean, powerful location-aware apps the right way.

Who this course is for:

  • iOS developers looking to integrate real-time geolocation features using SwiftUI.
  • SwiftUI learners who want to deepen their understanding of data flow and MVVM architecture.
  • Mobile developers aiming to build location-aware apps using CoreLocation and MapKit.
  • Intermediate Swift programmers ready to move from simple projects to practical, map-based applications.
  • App builders interested in combining asynchronous code with SwiftUI views.
  • Developers transitioning from UIKit to SwiftUI who want real-world project experience.
  • Freelancers looking to deliver apps that integrate maps, directions, and real-time data.
  • Educators and instructors seeking structured examples of advanced SwiftUI use cases.
  • Hobbyists with basic Swift knowledge who want to learn by building a functional location-based app.