Design+Code logo

Quick links

Suggested search

illustration

Create a weather app Part 1

icon

Add to favorites

Get the user's current location with CoreLocation

play icon

Play video

cancel
icon

Build Quick Apps with SwiftUI

1

Create a weather app Part 1

14:28

2

Create a weather app Part 2

9:52

3

Create a weather app Part 3

6:49

4

Create a To-Do app Part 1

12:51

5

Create a To-Do app Part 2

12:15

6

Create a To-Do app Part 3

17:27

7

Build a Biometrics Authentication System Part 1

9:54

8

Build a Biometrics Authentication System Part 2

14:41

9

Build a Biometrics Authentication System Part 3

14:16

10

Build a Trivia Game App Part 1

11:21

11

Build a Trivia Game App Part 2

17:38

12

Build a Trivia Game App Part 3

18:48

13

Build a Trivia Game App Part 4

13:39

14

Instagram Front-end UI Part 1

20:12

15

Instagram Front-end UI Part 2

20:06

16

Instagram Front-end UI Part 3

8:10

17

Instagram Front-end UI Part 4

27:51

18

Build a Recipe Saver App Part 1

13:31

19

Build a Recipe Saver App Part 2

9:02

20

Build a Recipe Saver App Part 3

6:40

21

Build a Recipe Saver App Part 4

8:38

22

Build a Recipe Saver App Part 5

8:12

23

Build a Recipe Saver App Part 6

18:07

24

Build a Recipe Saver App Part 7

6:57

25

Build a Recipe Saver App Part 8

5:09

26

Create a Shopping App with Apple Pay Part 1

12:16

27

Create a Shopping App with Apple Pay Part 2

12:03

28

Create a Shopping App with Apple Pay Part 3

9:08

29

Create a Shopping App with Apple Pay Part 4

13:21

30

Create a Shopping App with Apple Pay Part 5

18:57

31

Create a Shopping App with Apple Pay Part 6

7:42

32

Chat app with Firebase Firestore Part 1

18:35

33

Chat app with Firebase Firestore Part 2

13:33

34

Chat app with Firebase Firestore Part 3

16:39

35

Build a Fasting Timer App Part 1

12:42

36

Build a Fasting Timer App Part 2

7:55

37

Build a Fasting Timer App Part 3

19:01

38

Build a Fasting Timer App Part 4

9:22

39

Build a Fasting Timer App Part 5

7:47

40

Build a Video App Part 1

13:53

41

Build a Video App Part 2

14:24

42

Build a Video App Part 3

14:23

43

Build a Meditation App with AVKit Part 1

8:50

44

Build a Meditation App with AVKit Part 2

14:59

45

Build a Meditation App with AVKit Part 3

11:48

46

Build a Meditation App with AVKit Part 4

16:19

47

Build a Meditation App with AVKit Part 5

21:15

Outline

We'll first start by getting the user's current location coordinates using the CoreLocation framework from Apple. Then, we'll learn how to use these coordinates to get the current weather information with OpenWeather , a free weather API. Finally, we'll present all the data we fetched in the beautiful UI below.

Final weather app

Requirements

As we'll be building a mobile application with SwiftUI, you need to make sure to have Xcode 13 installed on your Mac, as well as an iPhone or iPad running on iOS 15 to test on. As you might have guessed it, we'll use the latest APIs and technologies introduced in iOS 15, so make sure to have Xcode and your phone updated to the latest versions. Also, as we'll require the user's location, we cannot test on the Simulator and will need to test on an actual device. Now, let's not waste any more time and start straight away!

Create an Xcode project

First, let's create a new Xcode project. In the modal that appears when you launch Xcode, click on Create a new Xcode project . Next, under iOS , select App . Click on Next.

New iOS App project in Xcode Give your project a name. Make sure the Interface is SwiftUI and the Language is set to Swift . Usually, these are the default values so you don't need to change anything.

Options for new Xcode project Then, we need to make sure to target devices on iOS 15 or higher. In other words, only devices running on iOS 15 will be able to download and use this application. Click on your project name, and under Targets , click again on your project name. Under the General tab > Deployment Info , select iOS 15.

Deployment Info target iOS 15 devices in Xcode If you want, you can also add an icon to your application by going into the Assets folder and, under AppIcon , add an icon for each empty slot. This will be the logo you'll see on your phone for the application you're currently developping. If you're a pro user, you can find the folder containing all these icons in the sources files of this section.

AppIcon Assets Xcode

Create a LocationManager

Now that the setup is done, we need to create a LocationManager . This will be the file in which we'll handle everything related to location. Create a new Group called Managers . Inside of it, add a new Swift file called LocationManager.swift.

New Swift file in Xcode At the top, let's import Foundation and CoreLocation .CoreLocation is the framework from Apple we'll use to get the user's current location.

// ./Managers/LocationManager.swift

import Foundation
import CoreLocation

Next, let's create the LocationManager class. It should conform to a NSObject , ObservableObject and CLLocationManagerDelegate.

// ./Managers/LocationManager.swift

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {

}

At the top of the class, add a manager. It's be a CLLocationManager instance.

// ./Managers/LocationManager.swift

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()
}

Below that, we'll add two published variables - a location and a loading state.

// ./Managers/LocationManager.swift

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()

    @Published var location: CLLocationCoordinate2D?
    @Published var isLoading = false
}

Finally, add the following functions to the class. They'll let us know the user's current location coordinates, and update the loading state.

// ./Managers/LocationManager.swift

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
		// Variables here...

    override init() {
        super.init()
        manager.delegate = self
    }

    func requestLocation() {
        isLoading = true
        manager.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.first?.coordinate
        isLoading = false
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error getting location", error)
        isLoading = false
    }
}

For the purposes of this tutorial, we'll request the user to share their location every time they launch the app. Of course, this is not the best user experience, but we want to keep this tutorial simple. You can take it a step further and always get the current location on the background, by reading Apple's documentation on the subject.

Get the coordinates

First, let's go to the ContentView . As we'll have a few views in our project, let's create a new group called Views , and drag and drop ContentView inside that new group. Then, inside of the struct , on the first line, initialize the LocationManager as a StateObject in ContentView.

// ./Views/ContentView.swift

@StateObject var locationManager = LocationManager()

Let's create a new view under the Views folder, in which we'll request the user to get their location. We'll call this the WelcomeView . At the top of the view, inside of the struct , we'll add the LocationManager as an environment object.

// ./Views/WelcomeView.swift

@EnvironmentObject var locationManager: LocationManager

Let's call the WelcomeView in the body of ContentView . Remember to add the environmentObject modifier to the WelcomeView as to not run into a crash later on. We'll also wrap it in a VStack , and set the background to a dark blue and the theme to dark.

// ./Views/ContentView.swift

VStack {
		WelcomeView()
				.environmentObject(locationManager)
}
.background(Color(hue: 0.656, saturation: 0.787, brightness: 0.354))
.preferredColorScheme(.dark)

Now, let's code the WelcomeView . It'll be a simple view with a title, a subtitle, and a button. Add the following code for the title and the subtitle.

// ./Views/WelcomeView.swift

VStack {
    VStack(spacing: 20) {
        Text("Welcome to the Weather App")
            .bold()
            .font(.title)

        Text("Please share your current location to get the weather in your area")
            .padding()
    }
    .multilineTextAlignment(.center)
    .padding()

}
.frame(maxWidth: .infinity, maxHeight: .infinity)

Next, we'll add a LocationButton right below the text. LocationButton is a new button introduced since iOS 15 that lets us request the user's location easily. We'll style the button with some cornerRadius , fill in the symbol, and add a white foregroundColor.

// ./Views/WelcomeView.swift

VStack {
    // Text here...

		LocationButton(.shareCurrentLocation) {
				locationManager.requestLocation()
		}
		.cornerRadius(30)
		.symbolVariant(.fill)
		.foregroundColor(.white)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)

You might run into an error that Xcode can't find LocationButton in scope. Import CoreLocationUI in the WelcomeView , as LocationButton comes with that framework.

// ./Views/WelcomeView.swift

import CoreLocationUI

Back in ContentView , let's display the coordinates of the user, to see if our code works. If we take a look at LocationManager , you'll see that we're saving the location in a published location variable. We can access it with locationManager.location in our View. As it is optional, we need to unwrap it.

// ./Views/ContentView.swift

if let location = locationManager.location {
	Text("Your coordinates are: \(location.longitude), \(location,latitude)")
}

Still in ContentView , add an else statement. While the app is getting the user's location, we'll add a ProgressView.

else {
		if locationManager.isLoading {
				ProgressView()
		} else {
				WelcomeView()
						.environmentObject(locationManager)
		}
}

Now connect your device to your Mac, build and run the app on your device, and see if you get a longitude and latitude displayed in your view. If you do, congratulations! We can now get the weather with these coordinates. Otherwise, scroll back up to see if you missed a step or forgot to add something in the code. Next, let's get the weather information in second section of this series!

READ NEXT

Create a weather app Part 2

Templates and source code

Download source files

Download the videos and assets to refer and learn offline without interuption.

check

Design template

check

Source code for all sections

check

Video files, ePub and subtitles

Videos

Assets

Subtitles

1

Create a weather app Part 1

Get the user's current location with CoreLocation

14:28

2

Create a weather app Part 2

Get the weather conditions depending on the user's location with the OpenWeather API

9:52

3

Create a weather app Part 3

Display the weather information in a beautiful UI

6:49

4

Create a To-Do app Part 1

Code all the views and components for a fully functional To-Do app

12:51

5

Create a To-Do app Part 2

Connect all the views together and install Realm

12:15

6

Create a To-Do app Part 3

Implement CRUD functionalities in the to-do app

17:27

7

Build a Biometrics Authentication System Part 1

Build the LoginView for authenticating with biometrics

9:54

8

Build a Biometrics Authentication System Part 2

Implement FaceID authentication in a SwiftUI application

14:41

9

Build a Biometrics Authentication System Part 3

Code the complete log in and log out flow of the app

14:16

10

Build a Trivia Game App Part 1

Create the project and code the welcome screen

11:21

11

Build a Trivia Game App Part 2

Build the QuestionView

17:38

12

Build a Trivia Game App Part 3

Create the Trivia model and TriviaManager

18:48

13

Build a Trivia Game App Part 4

Connect functions to UI and play the game

13:39

14

Instagram Front-end UI Part 1

Learn how to code Instagram home screen under 1h

20:12

15

Instagram Front-end UI Part 2

Learn how to code Instagram home screen under 1h

20:06

16

Instagram Front-end UI Part 3

Learn how to navigate between multiple screens in SwiftUI

8:10

17

Instagram Front-end UI Part 4

Learn how to use text field and dynamic grid layout in SwiftUI

27:51

18

Build a Recipe Saver App Part 1

Set up main views, tab bar and data model

13:31

19

Build a Recipe Saver App Part 2

Design recipe card using AsyncImage

9:02

20

Build a Recipe Saver App Part 3

Display recipe cards in a grid layout

6:40

21

Build a Recipe Saver App Part 4

Design recipe screen with picture and details

8:38

22

Build a Recipe Saver App Part 5

Filter and display recipes by categories

8:12

23

Build a Recipe Saver App Part 6

Present a form to submit a new recipe and allow for dismiss

18:07

24

Build a Recipe Saver App Part 7

Integrate a view model to manage data using Combine

6:57

25

Build a Recipe Saver App Part 8

Save a new recipe to collection

5:09

26

Create a Shopping App with Apple Pay Part 1

Create the Xcode project, add dummy data and build the ProductCard

12:16

27

Create a Shopping App with Apple Pay Part 2

Code the ContentView’s body and create the CartManager

12:03

28

Create a Shopping App with Apple Pay Part 3

Connect the CartManager to the UI

9:08

29

Create a Shopping App with Apple Pay Part 4

Code the Apple Pay button and configure Apple Pay

13:21

30

Create a Shopping App with Apple Pay Part 5

Create the PaymentHandler to handle everything related to Apple Pay

18:57

31

Create a Shopping App with Apple Pay Part 6

Connect Apple Pay with the UI

7:42

32

Chat app with Firebase Firestore Part 1

Create the Xcode project, and code the components making up the view

18:35

33

Chat app with Firebase Firestore Part 2

Code a custom TextField and configure Firebase

13:33

34

Chat app with Firebase Firestore Part 3

Call the data from Firestore with real-time updates

16:39

35

Build a Fasting Timer App Part 1

Design progress ring with animation

12:42

36

Build a Fasting Timer App Part 2

Implementing UI of the main view

7:55

37

Build a Fasting Timer App Part 3

Scheduling date with calendar date components

19:01

38

Build a Fasting Timer App Part 4

Tracking time with timer publisher

9:22

39

Build a Fasting Timer App Part 5

Calculate progress from timer and display in progress ring

7:47

40

Build a Video App Part 1

Code the QueryTag and the VideoCard

13:53

41

Build a Video App Part 2

Dive into the Pexels API, add dummy data and integrate a video player with AVKit

14:24

42

Build a Video App Part 3

Fetch the videos from the API and add functionality to UI

14:23

43

Build a Meditation App with AVKit Part 1

Download photo for background image and mount main screen

8:50

44

Build a Meditation App with AVKit Part 2

Implementing playback view UI and present as full screen cover

14:59

45

Build a Meditation App with AVKit Part 3

Set up data and view model integrating Combine and DateComponentsFormatter

11:48

46

Build a Meditation App with AVKit Part 4

Enable audio player and backroung playback

16:19

47

Build a Meditation App with AVKit Part 5

Add actions to playback controls and visualize audio progress

21:15

Test your knowledge of Build Quick Apps with SwiftUI. Complete the course and get perfect results for this test to get your certificate.