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.
To share any of our pictures we need to add a
navigationItem to the
DetailViewController. We do this by adding this line to
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystem: .action, target: self, action #selector(shareTapped))
Let’s break down this very quickly:
barButtonSystempart is set to
.actionbecause that is the sharing sheet that iOS provides us.
- the target is assigned to
selfas we want this view controller to be where the sharing sheet will appear.
- the action will use an Objective-C functionality via the
#selectorsyntax and it will call the
shareTappedmethod that we are about to write.
Here is the
guard letstatement is because
UIImageis optional here.
UIActivityViewControlleris 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.
popoverPresentationControllerline 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).
selfrefers 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.
#selectorallows Swift to check that a method exists while building our code.
- Activity view controllers must be presented from something on iPad (that is the
- The target of an action refers to where that method will be run.
- Xcode will refuse to build if you haven’t used
@objcwhen it’s needed.
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!