MemoryCap’s First Steps

For posterity’s sake, I’ll be documenting some of the process to writing the app here.

Showing the map

First, we add a mapview to Main.storyboard, and link the MapKit library. This creates the main view of our app, i.e. the map.

But the map is not very useful without our location, so we're going to use the CoreLocation library:

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {
...
}

We then need to get the user's location:

    override func viewWillAppear(_ animated: Bool) {
...
determineMyCurrentLocation()
}

func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}

Remember to add a "Privacy - Location Always Usage Description" key to `Info.plist` or ios won't be happy.
This is the message that displays when the app requests for the user's permission to access their location, and it won't do without it.

Also make sure that mapView actually shows the user's location:

override func viewDidLoad() {
...
mapView.showsUserLocation = true
}

Now we have a location, but our map still isn't very useful, so we tell the locationManager to zoom in on the user's location when the location is updated:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
let latitude:CLLocationDegrees = userLocation.coordinate.latitude
let longitude:CLLocationDegrees = userLocation.coordinate.longitude
let latDelta:CLLocationDegrees = 0.02
let lonDelta:CLLocationDegrees = 0.02
let span = MKCoordinateSpanMake(latDelta, lonDelta)
let location = CLLocationCoordinate2DMake(latitude, longitude)
let region = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
}

Adding images

We add a Navigation Controller to create an interface for the user to add images.
And add a bar button to open the image picker. 
And make the navigation bar transparent, because aesthetics. (We'll replace the text with an icon later)

override func viewDidLoad() {
...
// Transparent navigation bar
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
}

Like with the location library, we have to make sure to have a message for the permissions prompt. Add a value for "Privacy - Photo Library Usage Description" in Info.plist. Then add the protocols UIImagePickerControllerDelegate and UINavigationControllerDelegate to the ViewController class.

With the prerequesites out of the way, we add the photo picker. set its delegate as picker.delegate = self

let imagePicker = UIImagePickerController()

override func viewDidLoad() {
...
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
imagePicker.allowsEditing = false
}

We're not going to worry about adding camera functionality yet, let's restrict to only adding photos from the library for now.

Create an action for the capsule button that will launch the photo library.

@IBAction func createCapsule(_ sender: Any) {
present(picker, animated: true, completion: nil)
}

Finally, upload the photo when the user chooses the image.

func imagePickerController(_ imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
uploadPhoto(chosenImage)
}

We'll write the uploadPhoto function later. 
 
Firebase

Let's take a quick detour to setup Firebase, which will allow us to sync user capsules with the cloud service. Using Google's friendly Add Firebase to your iOS Project guide, we add Firebase to the project using Cocoapods.

After installing Cocoapods and adding Firebase to the project, modify AppDelegate.swift:

import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func application(...) -> Bool {
...
FIRApp.configure()
...
}
}

Also download the GoogleService-Info.plist from the Firebase project page and add it to the project.

Next, add Geofire to the project as well. Geofire 2.0 isn't supported directly by Cocoapods yet, so use a workaround.

To be continued…

Show your support

Clapping shows how much you appreciated Qubidt’s story.