Hacking with Swift – Learning Project 3

Here we are with a new project from the 100 Days of Code initiative. This time we are going to edit project 1. I think I have 3 copies of that project on my SSD so I deliberately did not follow the instructions on making yet another copy of it. Instead, I want to start from where we left it in the challenge and make it evolve from there.

Sharing data

To share any of our pictures we need to add a navigationItem to the DetailViewController. We do this by adding this line to viewDidLoad:

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystem: .action, target: self, action #selector(shareTapped))

Let’s break down this very quickly:

  • the barButtonSystem part is set to .action because that is the sharing sheet that iOS provides us.
  • the target is assigned to self as we want this view controller to be where the sharing sheet will appear.
  • the action will use an Objective-C functionality via the #selector syntax and it will call the shareTapped method that we are about to write.

Here is the shareTapped method:

Screen Shot 2019-02-23 at 20.32.50
  • The guard let statement is because UIImage is optional here.
  • The UIActivityViewController is very similar to the Alert View Controller we set up in the Guess the Flag game, it asks for an array of images to act on and on an array of activities, which we left empty.
  • The popoverPresentationController line is necessary because this kind of view controller on iPad has the shape of a comics box coming out of the share button.

Fixing the bug

Everything work but if we try to save image the app will crash because we didn’t ask permission to save it on our local storage. This is easy to fix: open the Info.plist and add the line Privacy – Photo Library Additions Usage Description with a text of “We need to save photos you like” (or something similar). That’s it.


That’s it for this project. Here is what we learned:

  • Activity view controllers can have custom application items.
  • Activity view controllers can share content to Twitter and Facebook (even if I think this has been removed somewhere, in Mojave for sure, maybe iOS is not there yet).
  • Image views may or may not have an image inside (as they are optionals).
  • self refers to whichever struct or class you’re currently inside.
  • We can share images, text, and more using activity view controllers.
  • Our app’s Info.plist file contains settings that won’t change (so we need to go there to perform deep changes to our app’s behaviour). I may add that on macOS I often open the Info.plist file of some annoying apps because they have the RunAtLoad key always set to true.
  • Activity view controllers can send our image using AirDrop.
  • #selector allows Swift to check that a method exists while building our code.
  • Activity view controllers must be presented from something on iPad (that is the popoverPresentationController.
  • @IBAction automatically implies @objc.
  • The target of an action refers to where that method will be run.
  • Xcode will refuse to build if you haven’t used @objc when it’s needed.

That’s it!

Now it’s time for a challenge.

Here is the GitHub project updated if you want this + the 1st Challenge together.

If you like what I’m doing here please consider liking this article and sharing it with some of your peers. If you are feeling like being really awesome, please consider making a small donation to support my studies and my writing (please appreciate that I am not using advertisement on my articles).

If you are interested in my music engraving and my publications don’t forget visit my Facebook page and the pages where I publish my scores (Gumroad, SheetMusicPlus, ScoreExchange and on Apple Books).

You can also support me by buying Paul Hudson’s books from this Affiliate Link.

Anyways, thank you so much for reading!

Till the next one!

Published by Michele Galvagno

Professional Musical Scores Designer and Engraver Graduated Classical Musician (cello) and Teacher Tech Enthusiast and Apprentice iOS / macOS Developer Grafico di Partiture Musicali Professionista Musicista classico diplomato (violoncello) ed insegnante Appassionato di tecnologia ed apprendista Sviluppatore iOS / macOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: