This is a weather forecast application that fetches real-time weather data from an API and displays it in a user-friendly interface. The app shows detailed information such as temperature, humidity, wind speed, and weather conditions, both by hour and by day of the week.
- Real-time Weather Data: The app fetches live weather information from an API.
- Hourly and Daily Forecast: Displays weather data in an hourly format as well as for the upcoming week.
- Temperature, Humidity, Wind Speed: Shows detailed weather statistics, including temperature, humidity, and wind speed.
- Weather Conditions: Provides a brief description of the weather conditions (e.g., clear, cloudy, rainy).
- Table View & Collection View: Displays weather data in a Table View for daily forecasts and a Collection View for hourly data.
- UI Components: Utilizes UI elements such as UILabel, icons, and images to create an engaging interface.
- Language: Swift
- Frameworks:
- UIKit for UI components like Table Views and Collection Views
- Foundation for data fetching and JSON decoding
- URLSession for API requests
The app fetches weather data from an external API (such as OpenWeatherMap, WeatherAPI, etc.). Data is fetched using URLSession, which handles HTTP requests and responses. After the data is received, it is decoded from JSON format using Swift's Codable protocol.
A UITableView is used to display the weather forecast for the upcoming days. Each cell contains information such as:
- Day of the week
- High and low temperatures
- Weather conditions (e.g., cloudy, sunny, etc.)
A UICollectionView displays hourly weather data, including:
- Hourly temperatures
- Wind speed
- Conditions (e.g., rain, snow, clear)
The weather data is dynamically updated based on the user's location and the API response. The app updates the displayed weather details in real time as the data is fetched.
Weather conditions are represented visually with icons and images to enhance the user experience. These are fetched from the API and displayed alongside the text data.
To manage your API key, latitude, and longitude securely, follow these steps:
-
In Xcode, create a new Configuration Settings File (
.xcconfig) by going to File > New > File. -
Name the file something like
Environment.xcconfig. -
Add your API key, latitude, and longitude to the
.xcconfigfile:WEATHER_API_KEY = your_api_key_here HOME_LATITUDE = your_latitude_here HOME_LONGITUDE = your_longitude_hereReplace
your_api_key_here,your_latitude_here, andyour_longitude_herewith your actual values.
- In Xcode, go to your project settings.
- Under Build Settings, add the
.xcconfigfile to the Debug and Release configurations.
You can access these environment variables in your app’s code using the Bundle class:
import Foundation
// Access the API key, latitude, and longitude
if let apiKey = Bundle.main.infoDictionary?["WEATHER_API_KEY"] as? String,
let latitude = Bundle.main.infoDictionary?["HOME_LATITUDE"] as? String,
let longitude = Bundle.main.infoDictionary?["HOME_LONGITUDE"] as? String {
print("API Key: \(apiKey)")
print("Latitude: \(latitude)")
print("Longitude: \(longitude)")
}A special thanks to bullas.atk@gmail.com for the helpful tutorial
