Day 73
Day 73 ๊ด๋ จ
Project 21, part two
As I write this, Apple is running a series of adverts with the tagline โif privacy matters in your life, it should matter to the phone your life is on.โ Of course, privacy does matter to the vast majority of us โ itโs a bit like saying โyou only need to floss the teeth you want to keep.โ
It makes sense that Apple is running these ads right now: with companies like Facebook, Amazon, Google, and more making a lot of money over data collection, Appleโs policy of putting users in control really sets the company apart from the pack in a really positive way.
However, this policy doesnโt end at the border of Cupertino. Youโre now a developer on Appleโs platforms, and it behoves you to attempt to follow in their footsteps: put the user in control, respect their settings, and treat their data with respect.
Today youโre going to be taking on some challenges to do with notifications, but when youโre doing it I want you to keep in mind what I said above: donโt abuse the trust users have placed in us, and remember that millions of apps have shipped before you that worked hard to maintain Appleโs high standards. Youโre part of that now and I hope you feel increasingly able to rise to match that heritage.
Today you should work through the wrap up chapter for project 21, complete its review, then work through all three of its challenges.
Wrap up
Wrap up
That was easy, right? And yet it's such a great feature to have, because now your app can talk to users even when it isn't running. You want to show a step count for how far they've walked? Use a notification. You want to trigger an alert because it's their turn to play in a game? Use a notification. You want to send them marketing messages to make them buy more stuff? Actually, just don't do that, you bad person.
Weโve only scratched the surface of what notifications can do, but if youโd like to explore more advanced topics โ such as attaching pictures or letting the user type responses rather than tapping buttons โ see my book Advanced iOS: Volume One.
Weโll be coming back to notifications again in project 33, where CloudKit is used to create and deliver remote notifications when server data has changed.
Review what you learned
Anyone can sit through a tutorial, but it takes actual work to remember what was taught. Itโs my job to make sure you take as much from these tutorials as possible, so Iโve prepared a short review to help you check your learning.
Click here to review what you learned in project 21.
Challenge
One of the best ways to learn is to write your own code as often as possible, so here are three ways you should try your new knowledge to make sure you fully understand whatโs going on:
- Update the code in
didReceive
so that it shows different instances ofUIAlertController
depending on which action identifier was passed in. - For a harder challenge, add a second
UNNotificationAction
to thealarm
category of project 21. Give it the title โRemind me laterโ, and make it callscheduleLocal()
so that the same alert is shown in 24 hours. (For the purpose of these challenges, a time interval notification with 86400 seconds is good enough โ thatโs roughly how many seconds there are in a day, excluding summer time changes and leap seconds.) - And for an even harder challenge, update project 2 so that it reminds players to come back and play every day. This means scheduling a week of notifications ahead of time, each of which launch the app. When the app is finally launched, make sure you call
removeAllPendingNotificationRequests()
to clear any un-shown alerts, then make new alerts for future days.
Wrap up - Additional
Thatโs another project finished, and one that teaches you a technique youโll come back to time and time again. Just remember to use your new-found power wisely!