-
Notifications
You must be signed in to change notification settings - Fork 149
/
15. Alerts, Notifications, Application Lifecycle.srt
executable file
·7902 lines (6309 loc) · 163 KB
/
15. Alerts, Notifications, Application Lifecycle.srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,401 --> 00:00:04,636
[MUSIC]
2
00:00:04,704 --> 00:00:10,009
Stanford University.
>> Well,
3
00:00:10,077 --> 00:00:13,745
welcome to Stanford CS193P,
fall of 2017 and
4
00:00:13,814 --> 00:00:18,016
18's Lecture number 15. And
today we have three topics for
5
00:00:18,084 --> 00:00:21,986
you, one is Alerts and Action
Sheets. So that's a way of
6
00:00:22,055 --> 00:00:25,290
kind of notifying the user
when an extraordinary event
7
00:00:25,358 --> 00:00:28,693
happens, or to help your user
make a branching decision
8
00:00:28,762 --> 00:00:32,364
where to go now in the app,
where it's kind of, you know,
9
00:00:32,433 --> 00:00:34,933
two or three choices
of where they can go.
10
00:00:35,001 --> 00:00:38,103
The second thing we're gonna
talk about is Notifications
11
00:00:38,172 --> 00:00:40,806
and KVO, that's a way of
communicating blind and
12
00:00:40,875 --> 00:00:43,408
structured inside of our
MVC and then finally,
13
00:00:43,477 --> 00:00:46,211
I'm gonna talk about
the Application Lifecycle.
14
00:00:46,280 --> 00:00:48,713
We already talked about
the view controller lifecycle,
15
00:00:48,782 --> 00:00:51,750
now we're gonna talk about the
lifetime of your application.
16
00:00:51,819 --> 00:00:53,384
All right, so
let's start with Alerts and
17
00:00:53,453 --> 00:00:56,421
Action Sheets. These two
things are very similar,
18
00:00:56,490 --> 00:00:59,357
the API to use them is almost
identical, so I always talk
19
00:00:59,426 --> 00:01:02,961
about them together. They're
a little bit different,
20
00:01:03,029 --> 00:01:05,831
an alert, it pops up in
the middle of the screen.
21
00:01:05,899 --> 00:01:08,667
They're both modal, they kinda
take over your screen but,
22
00:01:08,736 --> 00:01:10,001
alert pops up in the middle.
23
00:01:10,070 --> 00:01:13,538
Generally, whatever the alert
is telling you or asking you,
24
00:01:13,607 --> 00:01:17,175
you're only gonna have one or
maybe two choices to respond.
25
00:01:17,244 --> 00:01:20,245
It would be unusual to have an
alert, even with three buttons
26
00:01:20,313 --> 00:01:23,548
in it. It's usually for
asynchronous things,
27
00:01:23,617 --> 00:01:26,718
things like a network failure
or something that happens out
28
00:01:26,787 --> 00:01:29,921
of the ordinary. You don't
wanna use an alert as just
29
00:01:29,990 --> 00:01:33,224
a way to talk to your user and
ask them a yes or no question,
30
00:01:33,293 --> 00:01:35,927
things like that. Alerts are
pretty disruptive to the user
31
00:01:35,995 --> 00:01:38,529
interface experience because
they lock out the entire app
32
00:01:38,598 --> 00:01:41,433
and force you to deal
with this thing.
33
00:01:41,501 --> 00:01:43,134
So it's not a primary,
34
00:01:43,203 --> 00:01:45,103
kind of user
interface element,
35
00:01:45,171 --> 00:01:48,440
it's more of an emergency
situation, asynchronous,
36
00:01:48,509 --> 00:01:52,544
unusual, that kind of thing.
Now an action sheet is similar
37
00:01:52,613 --> 00:01:55,714
in that it's modal, takes over
the screen, but here you're
38
00:01:55,783 --> 00:01:59,084
almost always gonna have more
than two choices. This thing
39
00:01:59,152 --> 00:02:01,820
slides up from the bottom,
you're gonna usually have two,
40
00:02:01,889 --> 00:02:05,290
three, four, five choices,
even. And here what you're
41
00:02:05,359 --> 00:02:07,859
doing is asking the user to
make a branching decision.
42
00:02:07,928 --> 00:02:09,595
Maybe they've asked
to take a picture,
43
00:02:09,663 --> 00:02:11,729
to get a picture in their app,
and you wanna know,
44
00:02:11,798 --> 00:02:13,031
do you want it
from the camera or
45
00:02:13,100 --> 00:02:14,766
do you want it from
the photo library, or
46
00:02:14,835 --> 00:02:16,301
do you want it from
somewhere else?
47
00:02:16,369 --> 00:02:18,270
So they're kind of
branching right there and
48
00:02:18,338 --> 00:02:20,772
you can't really help them
until they make that decision
49
00:02:20,840 --> 00:02:23,441
for you, so that's what an
action sheet is for. It's not
50
00:02:23,510 --> 00:02:26,310
like an alert in that it's for
unusual circumstances,
51
00:02:26,379 --> 00:02:30,148
it's kind of normal branching
decisions that you would have
52
00:02:30,217 --> 00:02:32,450
to make. So here's what
these things look like,
53
00:02:32,519 --> 00:02:34,786
an action sheet slides up
from the bottom like that,
54
00:02:34,855 --> 00:02:37,322
and it can have pretty much
any number of buttons although
55
00:02:37,390 --> 00:02:40,291
obviously, you wouldn't want
it to fill the whole screen.
56
00:02:40,360 --> 00:02:42,193
And then an alert,
you've seen those,
57
00:02:42,262 --> 00:02:44,062
they come up in
the middle of the screen,
58
00:02:44,131 --> 00:02:47,499
they might even have a little
text field in them as well.
59
00:02:47,567 --> 00:02:50,068
So, again, the API for
them, i.e, using them,
60
00:02:50,137 --> 00:02:52,837
the code you write is almost
identical, so let's just
61
00:02:52,906 --> 00:02:55,807
talk about one of them first
which is actionSheet. Now,
62
00:02:55,875 --> 00:02:59,878
the way both of them work,
they're just UIViewController.
63
00:02:59,946 --> 00:03:01,112
There nothing
special about them,
64
00:03:01,181 --> 00:03:02,447
they're just
UIViewControllers.
65
00:03:02,516 --> 00:03:06,250
You know, we learned about how
to present a UIViewController
66
00:03:06,319 --> 00:03:10,655
modally in the last lecture,
when we put our emoji art
67
00:03:10,724 --> 00:03:12,890
controller document
up modally,
68
00:03:12,959 --> 00:03:16,027
it took over the whole
screen until we said done.
69
00:03:16,096 --> 00:03:17,429
Same thing here
with alerts and
70
00:03:17,498 --> 00:03:20,365
action sheets.
This UIAlertController is
71
00:03:20,433 --> 00:03:23,101
a UIViewController and we're
just gonna present it using
72
00:03:23,170 --> 00:03:26,003
exactly the same method we
did to present our document.
73
00:03:26,072 --> 00:03:28,807
It's called present, but we're
not gonna create it by getting
74
00:03:28,875 --> 00:03:31,276
it out the storyboard like
what we did with our document.
75
00:03:31,344 --> 00:03:33,945
We're gonna create it
with this initializer,
76
00:03:34,014 --> 00:03:37,582
UIAlertController title
message preferredStyle. And so
77
00:03:37,651 --> 00:03:40,118
the title is just the title
you can see on the left there,
78
00:03:40,187 --> 00:03:41,986
the image where
the title goes, right?
79
00:03:42,055 --> 00:03:45,290
It's the little thing at the
top Redeploy Cassini, and then
80
00:03:45,358 --> 00:03:48,393
the message goes below that
issue commands to Cassini's
81
00:03:48,462 --> 00:03:51,396
guidance system. And then
the preferredStyle is either
82
00:03:51,464 --> 00:03:54,766
actionSheet or alert. So,
this is what it looks like for
83
00:03:54,834 --> 00:03:58,503
actionSheet. Now, you
create your actionSheet and
84
00:03:58,572 --> 00:04:01,573
then you want to add actions,
these actions correspond to
85
00:04:01,641 --> 00:04:04,509
the buttons. So, we have four
actions right there in our
86
00:04:04,577 --> 00:04:07,745
example in what we're gonna
control Cassini here, and so
87
00:04:07,814 --> 00:04:10,648
we're gonna have four times
we're gonna call a addAction.
88
00:04:10,717 --> 00:04:14,519
The argument to addAction,
there's only one argument,
89
00:04:14,587 --> 00:04:18,390
it's a UIAlertAction.
Which is another object and
90
00:04:18,459 --> 00:04:20,992
let's take a look at
the initializer for
91
00:04:21,061 --> 00:04:23,728
UIAlertAction. It takes
a title, that's gonna be
92
00:04:23,797 --> 00:04:26,698
the title that appears on the
button, a style, we'll talk
93
00:04:26,767 --> 00:04:29,701
about the styles in a moment
here, and then a handler, and
94
00:04:29,770 --> 00:04:32,370
a handler is just a closure
you're gonna give it. It takes
95
00:04:32,439 --> 00:04:35,106
the action as an argument so
that it's in your right hand,
96
00:04:35,174 --> 00:04:37,809
and you're gonna do whatever
you do when that button gets
97
00:04:37,877 --> 00:04:39,777
clicked. So,
this alert in action,
98
00:04:39,846 --> 00:04:44,549
sheets have a very simple API,
you just put these buttons and
99
00:04:44,618 --> 00:04:47,585
the code that goes
with them on each one.
100
00:04:47,654 --> 00:04:51,590
So this handler for, our first
button there, Orbit Saturn,
101
00:04:51,658 --> 00:04:54,125
was just kind of a normal
button, so its style is
102
00:04:54,194 --> 00:04:58,230
UIAlertActionStyle.default.
And the handler in there,
103
00:04:58,298 --> 00:05:01,032
I'm just going to go into
orbit around Saturn. And
104
00:05:01,101 --> 00:05:03,969
that's someone, presumably,
pressed this button once,
105
00:05:04,038 --> 00:05:06,137
and Cassini did that,
it's finished now,
106
00:05:06,205 --> 00:05:08,974
but it did that at one point.
So this is just the normal
107
00:05:09,042 --> 00:05:12,143
default, action here to
orbit Saturn, right?
108
00:05:12,211 --> 00:05:15,279
And I can add another action,
maybe Explore Titan, now,
109
00:05:15,348 --> 00:05:17,582
okay that's not
Cassini's mission, but
110
00:05:17,651 --> 00:05:20,751
maybe we could allow it if we
let, make the person logins.
111
00:05:20,820 --> 00:05:24,222
So in the handler there, I've
asked them to log in to verify
112
00:05:24,290 --> 00:05:28,627
they really want to divert
Cassini to Titan, right?
113
00:05:28,695 --> 00:05:31,563
How about the next one, which
is the Closeup of the Sun?
114
00:05:31,632 --> 00:05:34,265
Well if I really wanted to
send, this Cassini off to
115
00:05:34,334 --> 00:05:37,068
the Sun, that would almost
certainly destroy Cassini.
116
00:05:37,137 --> 00:05:40,304
And that's why it has a little
different style there, which
117
00:05:40,373 --> 00:05:43,908
is .destructive, you see where
it says style .destructive? So
118
00:05:43,977 --> 00:05:48,313
a .destructive item is gonna
show up in red text right
119
00:05:48,382 --> 00:05:52,116
there and it's any choice
that is going to make us,
120
00:05:52,185 --> 00:05:57,288
kind of significant
unrecoverable action happen.
121
00:05:57,357 --> 00:05:58,573
you're gonna delete
something out of a database.
122
00:05:58,574 --> 00:05:59,790
For example,
123
00:05:59,859 --> 00:06:02,961
That might be a destructive
action, obviously crashing
124
00:06:03,029 --> 00:06:06,497
Cassini into the Sun, that
would be a destructive action,
125
00:06:06,566 --> 00:06:09,301
so that's what .destructive
style is there. And
126
00:06:09,369 --> 00:06:11,636
then, finally, we have this
cancel action at the bottom,
127
00:06:11,705 --> 00:06:13,271
you see it's kinda
separated a little?
128
00:06:13,340 --> 00:06:14,572
And its style is different,
129
00:06:14,641 --> 00:06:17,642
.cancel, that's what it causes
it to be separated. But a very
130
00:06:17,710 --> 00:06:20,778
important other thing about
.cancel is that on the iPad,
131
00:06:20,847 --> 00:06:23,849
this thing is not gonna slide
up from the bottom. It's gonna
132
00:06:23,917 --> 00:06:25,917
appear in a popover,
which we haven't talked about,
133
00:06:25,985 --> 00:06:27,652
and we're gonna talk
all about on Wednesday.
134
00:06:27,720 --> 00:06:31,022
But a popover is also modal,
kinda takes over the screen,
135
00:06:31,091 --> 00:06:33,825
but it appears in a little
window and, in there,
136
00:06:33,894 --> 00:06:35,726
you can't have
a cancel button,
137
00:06:35,795 --> 00:06:39,398
cuz a popover is cancelled
by touching somewhere else.
138
00:06:39,466 --> 00:06:40,932
A popover comes up,
if you wanna cancel it,
139
00:06:41,001 --> 00:06:42,834
you touch somewhere else so
you don't need a cancel,
140
00:06:42,903 --> 00:06:45,704
you don't wanna cancel button.
That's why it's important to
141
00:06:45,772 --> 00:06:48,406
note which of your buttons
are a cancel button, so
142
00:06:48,475 --> 00:06:50,375
that when this
appears in a popover,
143
00:06:50,443 --> 00:06:54,446
the action sheet here won't
include that cancel buttons.
144
00:06:54,514 --> 00:06:56,948
All right, so
we've added our four buttons,
145
00:06:57,017 --> 00:06:59,584
now we want this thing to
slide up to the bottom and
146
00:06:59,653 --> 00:07:02,154
let the user choose and
execute the appropriate
147
00:07:02,222 --> 00:07:04,589
handler and we do that
with that same method,
148
00:07:04,658 --> 00:07:07,625
present. It's a ViewController
method, present,
149
00:07:07,694 --> 00:07:10,595
presumably the ViewController
behind there, you can see that
150
00:07:10,664 --> 00:07:13,565
it's got the picture of
Cassini. That ViewController
151
00:07:13,634 --> 00:07:16,367
is presenting this action
sheet and it's, you know,
152
00:07:16,436 --> 00:07:19,870
present the view controller,
animated true, almost always,
153
00:07:19,939 --> 00:07:22,974
and then completion handler
if you want for which will
154
00:07:23,043 --> 00:07:26,277
get called when the animation
finishes. So that's it,
155
00:07:26,346 --> 00:07:27,712
super easy to use an alert.
156
00:07:27,781 --> 00:07:31,349
Now let's talk about, this is
an action sheet, by the way.
157
00:07:31,417 --> 00:07:34,285
So let's talk about what it
looks like on iPad, though,
158
00:07:34,354 --> 00:07:37,522
I said it was a popover, this
is is what it would look like.
159
00:07:37,590 --> 00:07:40,091
In this case, you see that
little redeploy button?
160
00:07:40,160 --> 00:07:41,959
And it's kinda small font, but
161
00:07:42,028 --> 00:07:44,762
it says redeploy in
the upper right of the iPad.
162
00:07:44,831 --> 00:07:48,633
When I click that, this
action sheet presented well,
163
00:07:48,701 --> 00:07:51,669
first of all how do I get
it to present like that?
164
00:07:51,738 --> 00:07:53,905
And then second of all,
how do I get it to point to
165
00:07:53,974 --> 00:07:56,808
the little redeploy button.
So I have to do both of those
166
00:07:56,877 --> 00:08:00,111
things. The first thing I'm
going to do is make it present
167
00:08:00,179 --> 00:08:03,114
like a popover. And
to do that, I set the alerts,
168
00:08:03,183 --> 00:08:05,616
which is a view control
remember, I set
169
00:08:05,685 --> 00:08:10,188
it's modalPresentationStyle to
be .popover. And that's gonna
170
00:08:10,256 --> 00:08:12,890
make it to represent as
a popover. But that's not
171
00:08:12,959 --> 00:08:15,593
quite enough because I have to
make a point at that little
172
00:08:15,662 --> 00:08:18,463
redeploy bar button item.
And the way I do that
173
00:08:18,532 --> 00:08:21,999
is I get the alerts popover
presentation controller,
174
00:08:22,068 --> 00:08:25,504
which will be nil if you're
not presenting it as popover.
175
00:08:25,572 --> 00:08:28,239
And one of the vars in
a popover presentation
176
00:08:28,307 --> 00:08:31,242
controller is the barButton
item that is presenting it.
177
00:08:31,311 --> 00:08:33,077
You can also specify
a rectangle,
178
00:08:33,146 --> 00:08:34,478
an arbitrary rectangle and
179
00:08:34,547 --> 00:08:38,083
the little triangle will point
at that when it appears.
180
00:08:38,151 --> 00:08:41,085
You can also specify which
direction the little arrow is
181
00:08:41,154 --> 00:08:45,056
allowed to point, left, right,
up or down. And in that way
182
00:08:45,125 --> 00:08:48,693
you can control the way
that this popover presents.
183
00:08:48,761 --> 00:08:51,295
Does this make sense?
Now you're probably saying,
184
00:08:51,364 --> 00:08:56,267
well I need to put if I'm on
iPad then do this around this,
185
00:08:56,335 --> 00:08:58,169
right? Because obviously
on an iPhone I don't want
186
00:08:58,237 --> 00:09:01,038
this thing to be a popover.
But actually, you don't have
187
00:09:01,107 --> 00:09:04,509
to do that, because this exact
same code will still slide up
188
00:09:04,577 --> 00:09:07,712
from the bottom on an iPhone.
Now, why does that happen?
189
00:09:07,781 --> 00:09:10,614
We've specifically asked for
the popover presentation style
190
00:09:10,683 --> 00:09:13,518
there and we specified the
barButton item and all that.
191
00:09:13,587 --> 00:09:18,089
Well, iOS automatically does
what we called adapting
192
00:09:18,158 --> 00:09:21,359
to the environment its in.
Both the trait environment,
193
00:09:21,428 --> 00:09:23,360
right, compact width and
or not, and
194
00:09:23,429 --> 00:09:26,497
all that stuff we talked about
before, but also the platform
195
00:09:26,566 --> 00:09:29,567
its on to some extent.
And so here it notices,
196
00:09:29,636 --> 00:09:33,337
well I'm on an iPhone,
I don't do popovers. Actually,
197
00:09:33,406 --> 00:09:36,474
you can do popovers on iPhone
as we'll talk about, but
198
00:09:36,542 --> 00:09:39,244
you have to do extraordinary
things to do that. So
199
00:09:39,312 --> 00:09:40,912
in the normal case of events
it doesn't do popover,
200
00:09:40,981 --> 00:09:44,615
so it comes over in
it's normal way. So
201
00:09:44,684 --> 00:09:45,850
if you're doing a popover, and
202
00:09:45,919 --> 00:09:48,286
you have a universal app that
runs on both iPad and iPhone,