Stanley-Moukhametzianov / Swiftui-NASA-Mars-Rover-Images-App

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

⚑ Swiftui-NASA-Mars-Rover-Images-App

  • App using Swift 5, to view data from the official NASA API.
  • Uses a grid to display the latest Mars images from the perseverance rover.
  • Note: to open web links in a new window use: ctrl+click on link

πŸ“„ Table of contents

πŸ“š General info

  • NASA Mars Rover Photos from the NASA API. Note video function requires npm module safe-pipe
  • NASA Mars Photos API github repo
  • This mobile app was created following the MVVM framework. The Model is responsible for making calls to the API and fetching the json data.

πŸ“· Screenshots

πŸ’Ύ App Setup

  • Get yourself a NASA API key from Nasa
  • Once you copy the Xcode peoject you will still need to create an additional swift file. The file should be called Constrant.swift and have the code
import Foundation

class Constrant{
    static var url : URL{
        guard let url = URL(string: "https://api.nasa.gov/mars-photos/api/v1/rovers/perseverance/latest_photos?api_key=") else{ fatalError()}
        
        return url
        
    }
    
    static var session:URLSession {
        let session = URLSession(configuration: .default)
        return session
        
    }
    static var request: URLRequest{
        let req = URLRequest(url: url)
        return req
        
    }
    
    static var decoder: JSONDecoder{
        
        let decoder = JSONDecoder()
        decoder.keyDecodingStrategy = .convertFromSnakeCase
        return decoder 
    }   
}

πŸ’» Code Examples

  • API.swift extract: gets data from Nasa API using APIKEY supplied by them.
import Foundation

class API {
    class func getImage(_ onSuccess: @escaping ([LatestPhoto]) -> ()){
        Constrant.session.dataTask(with: Constrant.request){(data, res, err) in
            guard let data = data, err == nil else{
                fatalError()
            }
            do{
                let apod = try JSONDecoder().decode(LatestPhotosWrapper.self, from: data)
                
                
                DispatchQueue.main.async {
                    onSuccess(apod.latestPhotos)
                }
                
            }
            catch{
                print(error)
            }
            
        }.resume()
        
    }
}

πŸ†’ Features

  • If you want to save any of the images to your iphone photo library you can do so by holding down on the image for two seconds. Then you will be asked for permission to access your photos and the image will be saved.

πŸ“ License

  • This project is licensed under the terms of the MIT license.

βœ‰οΈ Contact

About


Languages

Language:Swift 100.0%