A simple iOS weather app that allows users to search for cities and view weather forecasts using the OpenWeatherMap API. The app is built with the MVVM architecture pattern and integrates with the OpenWeatherMap API for geocoding and weather forecasting.
weather.demo.mp4
- Search for cities using the OpenWeatherMap Geocoding API.
- Fetch current weather and 5-day weather forecasts for the selected city.
- Display daily weather for the next 5 days.
- Swift 5: Programming language for iOS development.
- UIKit: Used for building the user interface.
- MVVM Architecture: To ensure separation of concerns and a scalable design.
- OpenWeatherMap API: For fetching weather and forecast data.
- URLSession: To handle network requests.
- XCTest: For writing unit tests.
- Xcode 12.0+: Ensure you have Xcode installed on your machine.
- iOS 13.0+: The app targets iOS 13 and above.
- OpenWeatherMap API Key: You will need an API key from OpenWeatherMap.
Open a terminal and run the following command to clone the project:
-
Clone the repository:
- git clone https://github.com/shubhamgupta0910/WeatherApp.git
- cd WeatherApp
-
Install dependencies: pod install
-
Build and Run the Project:: open WeatherApp.xcworkspace
-
Run the project In Xcode:
- Select the target simulator or device.
- Click Run (or use the shortcut Cmd + R).
The app will launch, allowing you to search for cities and view weather data.
This project includes unit tests to validate the functionality of the ViewModel classes and their interaction with the WeatherService.
Running Tests:
- Open Xcode.
- Select the Test option from the Product menu or press Cmd + U.
Generate Test Reports: To generate a test coverage report:
Go to Xcode -> Preferences -> Locations. Under Derived Data, click the arrow to open the derived data folder. Once the tests run, a test report will be available inside the DerivedData directory.
The app uses the OpenWeatherMap API to fetch weather data. Create an account on OpenWeatherMap and obtain your API key.
Geocoding API https://api.openweathermap.org/geo/1.0/direct?q={city}&limit=5&appid={API_KEY}
Weather Forecast API https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={API_KEY}&units=metric
Project Architecture: MVVM Pattern The Model-View-ViewModel (MVVM) pattern was chosen for this project to improve separation of concerns, testability, and maintainability.
Handling APIs:
- All the network requests are handled in a separate WeatherService class to isolate the networking logic. This improves testability and follows the Single Responsibility Principle.
- The OpenWeatherMap API is used for both geocoding and weather data.
Data Parsing and Processing: The app processes the weather forecast by grouping the data into daily summaries. The ViewModel manages the transformation of raw API data into a format suitable for UI display (e.g., calculating daily min/max temperatures).
Error Handling: Error handling is done using callback closures. If there is an error, ensuring that error messages can be displayed to the user.