Hacking with Swift – Challenge 3

These are today’s challenges for Project3 (which is Project1 updated)

  1. Try adding the image name to the list of items that are shared. The activityItems parameter is an array, so you can add strings and other things freely. Note: Facebook won’t let you share text, but most other share options will.
  2. Go back to project 1 and add a bar button item to the main view controller that recommends the app to other people.
  3. Go back to project 2 and add a bar button item that shows their score when tapped.

Image name

I will be sincere: I do not understand what is the point of this. If I share the image and, then, with it, I share its name … where will it be stored? How will it be showed?

I tried to add a custom string to the array but once I built the app nothing appeared anywhere.

The thing that made me click was that running in the Xcode simulator is like driving a car without wheels, you can only imagine what it can do.

I therefore added a “TestString” to the array of activityItems and built on my device, an iPhone XS. Once I managed it, I tapped on the share button and I finally got back the option to send it via iMessage. Tapping that brought up the Message app interface with the selected picture and the “TestString” message in the text. Bingo! Now let’s have it show the name of the picture instead.

In the end it was easier than what I thought: I just needed to add, in the array, selectedImage! (yes, with the exclamation mark because it is an optional but we are sure the image is there because we would have not selected it otherwise!).

This is done!

Recommending the app

The first idea I got for this challenge was the following:

Screen Shot 2019-02-23 at 21.13.06

Of course this is not doing anything because the nil is attached to the action parameter. Still I got the button to display “Share this with your friends!”.

Now I’m asking myself: what does the challenge want? And this always drives me nuts… most of the times I have no problem to go through these challenges but to understand the question. They are so ambiguous sometimes. Where should I suggest it to? How? Which is the “correct” way? I really do not like the “there is no correct or wrong” approach.

Let’s suppose I am just trying to suggest people to try my app via Message… I could use a similar action sheet as before and just pass in the String with a GitHub link. I do not have a link to the App Store of course so that is not feasible. Paul said that Facebook will not let us share Strings, so I hope that option will be off by default.

Let’s do that: first of all let’s create a navigationItem as before:

Screen Shot 2019-02-23 at 21.28.31

suggest is the name of the method I will call and here is the method:

Screen Shot 2019-02-23 at 21.29.34
PS: I like scopes! 😛

I tried it a few seconds ago and, curiously, it lets me post it on Facebook as well. Bug? Intentional? Who cares? It works! I posted it on Twitter as well!

I am curiously, though, how you out there solved it. Please let me know!

Show the score!

Now let’s head back to Project 2, the Guess the Flag game.

First thing: now we can see the score already in the title and this is not good for this challenge. So we need to comment out the title line in the askQuestion method and replace it with what we had before: title = uppercasedCountry.

Now … I really wanted to show this as a popover but it seems that on iPhone this is not possible. I therefore chose an actionSheet-styled alert.

In viewDidLoad() I created a navigationItem, this time attached to the leftBarButtonItem (I liked it more this way) and chose a title of “Score”, a style of .plain, a target of self and an action of #selector(showScore).

I then wrote the showScore method:

Screen Shot 2019-02-23 at 22.22.05

Now, the result is not beautiful because SCORE is very little and the button about the current score is quite large, but also having the score as a message in the UIAlertController and an action of continue didn’t satisfy me.

The app works as intended, just its design is not as beautiful as I would like it to be.

I am curious to know how you solved this.

Please let me know in the comments below!

Happy coding!

GitHub link to the updated project.

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: