-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy path14. Persistence and Documents Demo.srt
7495 lines (5981 loc) · 155 KB
/
14. Persistence and Documents Demo.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,603
[MUSIC]
2
00:00:04,671 --> 00:00:09,474
Stanford University.
>> All right,
3
00:00:09,543 --> 00:00:14,046
well, welcome to lecture
number 14 of Stanford CS193P,
4
00:00:14,114 --> 00:00:18,884
fall of 2017, 18. So, I'm
going to finish off the thing
5
00:00:18,953 --> 00:00:22,254
we had about the documents
last time, I was hoping to
6
00:00:22,323 --> 00:00:24,489
have finished that last time.
So we're kind of rushed for
7
00:00:24,558 --> 00:00:27,025
time this time, especially
since we're gonna get kicked
8
00:00:27,093 --> 00:00:29,794
out again for a midterm right
after. And then I'm gonna
9
00:00:29,863 --> 00:00:32,364
launch into a long demo that's
gonna basically demonstrate
10
00:00:32,432 --> 00:00:35,300
all of the stuff we
talked about on Monday.
11
00:00:35,368 --> 00:00:39,504
All right, so we did all that
stuff on Monday so that we
12
00:00:39,573 --> 00:00:43,075
could easily take whatever
our document-based app is,
13
00:00:43,143 --> 00:00:45,743
and store it in a document.
We went all the way through
14
00:00:45,812 --> 00:00:49,314
having UIDocument write things
out to the file system.
15
00:00:49,383 --> 00:00:52,751
Now we're gonna add a really
cool layer of UI in the front
16
00:00:52,819 --> 00:00:55,754
of that, so that users can
pick the documents they want.
17
00:00:55,822 --> 00:00:59,024
Rename them, move them around,
all that right inside your
18
00:00:59,093 --> 00:01:02,327
app. And the UI for this, as
you can see the screenshot of,
19
00:01:02,395 --> 00:01:05,597
looks a lot like the files
app in iOS 11. In fact,
20
00:01:05,666 --> 00:01:09,034
the files app in iOS 11 is
probably just a very thin
21
00:01:09,103 --> 00:01:12,104
layer on top of this UI,
or this API,
22
00:01:12,172 --> 00:01:15,340
the
UIDocumentBrowserViewControl-
23
00:01:15,408 --> 00:01:17,709
ler. So
how does this thing work,
24
00:01:17,777 --> 00:01:20,378
this view controller? Well,
the most important thing to
25
00:01:20,447 --> 00:01:22,447
understand is that it's
just a view controller, but
26
00:01:22,516 --> 00:01:25,183
it's always the root
view controller of
27
00:01:25,251 --> 00:01:27,452
your application. So in other
words, in your storyboard,
28
00:01:27,521 --> 00:01:30,122
it's always got the little
arrow that points to it. And
29
00:01:30,190 --> 00:01:31,323
when you run your app,
30
00:01:31,391 --> 00:01:33,926
it's gonna be the view
controller users see first.
31
00:01:33,994 --> 00:01:37,062
Then they'll pick whatever
document they want, and
32
00:01:37,131 --> 00:01:38,797
then your UI will take over.
33
00:01:38,865 --> 00:01:41,166
Your view controller will
come up in front of this one,
34
00:01:41,234 --> 00:01:43,769
do whatever it does to show
your document. All right, so
35
00:01:43,837 --> 00:01:47,305
that's the fundamental
architecture of this thing.
36
00:01:47,374 --> 00:01:50,676
And one thing that's important
to tell this little document
37
00:01:50,744 --> 00:01:54,446
browser view controller is
what types of files you open,
38
00:01:54,514 --> 00:01:56,080
and you do that on your
project settings. So
39
00:01:56,149 --> 00:01:59,451
if you go to project settings
underneath the info tab,
40
00:01:59,519 --> 00:02:01,686
you gotta select your target,
not the project, but
41
00:02:01,755 --> 00:02:04,155
the target. Go to the info
tab, and then inside there,
42
00:02:04,224 --> 00:02:06,225
there's a little section
there called document types.
43
00:02:06,293 --> 00:02:08,793
Well, that's the types of
documents you open, and
44
00:02:08,862 --> 00:02:09,961
you just fill out this.
45
00:02:10,030 --> 00:02:12,564
You can have multiple types
when you fill out this little
46
00:02:12,633 --> 00:02:14,800
form, like that one.
In here, for example,
47
00:02:14,868 --> 00:02:17,468
I've shown how to have JSON
files be a kind of file that
48
00:02:17,537 --> 00:02:20,505
you open, and it's pretty
straightforward there.
49
00:02:20,574 --> 00:02:23,108
The little things at the
bottom there, the BundleType
50
00:02:23,177 --> 00:02:25,644
and the HandleRank, are
basically just saying, well,
51
00:02:25,712 --> 00:02:28,613
is this like a primary type
of file that you open?
52
00:02:28,682 --> 00:02:32,217
Are you the owner of this type
of file, or is it just some
53
00:02:32,285 --> 00:02:35,587
other type that you can,
that you know how to open,
54
00:02:35,656 --> 00:02:38,456
etc. Now, you can have
your own document type,
55
00:02:38,525 --> 00:02:39,824
for example, for emojiart,
56
00:02:39,893 --> 00:02:42,594
I'm not gonna get to it today
because of time constraints.
57
00:02:42,662 --> 00:02:46,531
But emojiart really wants to
have its own document type,
58
00:02:46,599 --> 00:02:50,002
.emojiart or something like
that. And you set that up in
59
00:02:50,070 --> 00:02:52,704
the section right below
the document types,
60
00:02:52,772 --> 00:02:56,108
called exported UTIs, UTI
stands for Universal Type and
61
00:02:56,176 --> 00:02:58,610
Identifier. And
you can invent your own UTI,
62
00:02:58,679 --> 00:03:00,578
as long as it's a nice,
unique name.
63
00:03:00,647 --> 00:03:04,549
Like here, I've used the name
edu.stanford.cs193p.emojiart,
64
00:03:04,617 --> 00:03:08,153
that's very likely to be
a unique type identifier.
65
00:03:08,222 --> 00:03:09,955
I just have to specify
a couple of things,
66
00:03:10,023 --> 00:03:11,723
most notably at
the bottom there,
67
00:03:11,792 --> 00:03:14,592
the file name extension that
goes with an emoji art,
68
00:03:14,661 --> 00:03:19,631
I picked emojiart, .emojiart.
And then once you set this up,
69
00:03:19,699 --> 00:03:22,400
you can go back up to the
document types one at the top,
70
00:03:22,469 --> 00:03:25,137
and add this as a document
type that you open.
71
00:03:25,205 --> 00:03:28,706
Just like that, and yeah, and
I explained the UTI there.
72
00:03:28,775 --> 00:03:33,845
Now we, you know,
from the start of this class,
73
00:03:33,914 --> 00:03:37,082
we always picked single view
app as the type of app that we
74
00:03:37,151 --> 00:03:39,617
opened. But we're actually,
for document based apps,
75
00:03:39,686 --> 00:03:41,452
we're going to use
an Xcode template.
76
00:03:41,521 --> 00:03:43,888
It doesn't really give us
much in that template,
77
00:03:43,957 --> 00:03:46,024
but it gives enough
that it's worth doing.
78
00:03:46,092 --> 00:03:47,959
So if you're gonna do
a document based app,
79
00:03:48,028 --> 00:03:52,430
I recommend going back and
creating a new app,
80
00:03:52,499 --> 00:03:55,200
a new project, using this as
the template. And in fact,
81
00:03:55,269 --> 00:03:57,635
I'm gonna do that in the demo,
even though we're, you know,
82
00:03:57,704 --> 00:03:59,037
80% of the way
through emojiart,
83
00:03:59,106 --> 00:04:00,105
I'm still gonna go back and
84
00:04:00,174 --> 00:04:01,539
create a new emojiart
with this, and
85
00:04:01,608 --> 00:04:02,707
then just move my files over.
86
00:04:02,776 --> 00:04:04,809
Because I want the things that
come with the template, and
87
00:04:04,878 --> 00:04:06,444
let's talk about what's
in that template.
88
00:04:06,513 --> 00:04:08,647
One thing is that
it stubs out that
89
00:04:08,716 --> 00:04:11,883
document types, and
it doesn't do JSON files.
90
00:04:11,952 --> 00:04:14,819
It does image files, but you
can change it to JSON files or
91
00:04:14,888 --> 00:04:17,622
add more types.
It also puts an entry in your
92
00:04:17,690 --> 00:04:19,624
Info.plist which
is very important,
93
00:04:19,693 --> 00:04:22,594
called Supports Document
Browser. And that's basically
94
00:04:22,663 --> 00:04:25,263
just saying that the documents
that are stored inside
95
00:04:25,332 --> 00:04:28,132
your app sandbox are like real
documents that people would
96
00:04:28,201 --> 00:04:31,403
wanna open with like the files
app or something like that. So
97
00:04:31,472 --> 00:04:33,605
that's an important entry
to put in your Info.plist,
98
00:04:33,673 --> 00:04:35,574
we're actually going to put
that into the demo manually,
99
00:04:35,642 --> 00:04:37,308
just to emphasize it.
100
00:04:37,377 --> 00:04:39,777
It has one method in
AppDelegate, you know,
101
00:04:39,846 --> 00:04:41,979
AppDelegate's that file that
we're always moving out of
102
00:04:42,048 --> 00:04:45,417
the way, into supporting
files. So it has one method in
103
00:04:45,485 --> 00:04:48,619
there that allows other apps,
like the files app,
104
00:04:48,688 --> 00:04:51,990
to ask your app to open your
documents. And it's a very
105
00:04:52,058 --> 00:04:54,292
simple little method, we're
not really gonna look at it
106
00:04:54,361 --> 00:04:57,195
today, but this template does
throw that in there for you.
107
00:04:57,264 --> 00:04:59,664
It also stubs out a subclass
of UIDocument, which you
108
00:04:59,733 --> 00:05:02,868
usually don't need, cuz you've
already created that yourself.
109
00:05:02,936 --> 00:05:05,970
And it also stubs out an MVC
to show the document,
110
00:05:06,039 --> 00:05:08,740
just this really dumb MVC. And
you don't need that, usually,
111
00:05:08,809 --> 00:05:11,609
either, because you obviously
have an MVC that shows your
112
00:05:11,678 --> 00:05:15,046
document, or you wouldn't be
writing this app. And finally,
113
00:05:15,115 --> 00:05:17,882
it gives a subclass of
UIDocumentBrowserViewControl-
114
00:05:17,951 --> 00:05:19,650
ler, which serves as
its own delegate.
115
00:05:19,719 --> 00:05:22,054
It just has like five methods
in it that are really simple,
116
00:05:22,122 --> 00:05:24,456
and we're gonna go over
those in detail. So
117
00:05:24,525 --> 00:05:26,891
after you create a project
with this template,
118
00:05:26,960 --> 00:05:29,094
what do you need to do to get
your app working? Well, one,
119
00:05:29,162 --> 00:05:31,929
you've gotta have your own
UIDocument subclass, of
120
00:05:31,998 --> 00:05:36,701
course. Two, you've gotta have
your own document-viewing MVC,
121
00:05:36,769 --> 00:05:38,703
right?
Like EmojiArtViewController,
122
00:05:38,772 --> 00:05:40,872
or your
ImageGalleryViewController,
123
00:05:40,941 --> 00:05:44,509
whatever from your homework.
Then you have to add
124
00:05:44,577 --> 00:05:48,046
a little bit of code to this
UIDocumentBrowserViewContro-
125
00:05:48,114 --> 00:05:51,282
ller subclass that comes
with the template. In its
126
00:05:51,351 --> 00:05:53,551
viewDidLoad, you're gonna
configure it a little bit,
127
00:05:53,620 --> 00:05:55,052
it has a couple of
bars you can set,
128
00:05:55,121 --> 00:05:58,289
we'll talk about that. You're
also going to have to provide
129
00:05:58,358 --> 00:06:01,493
the URL of a blank document.
And
130
00:06:01,561 --> 00:06:03,962
this is so that when the user
goes into the UI and
131
00:06:04,030 --> 00:06:07,332
says, create a new document,
you've got, it's gotta
132
00:06:07,400 --> 00:06:10,902
copy some template, some blank
version of your document,
133
00:06:10,971 --> 00:06:13,705
to be that new document. So
you just have to give the URL
134
00:06:13,774 --> 00:06:17,141
of a blank document, so that
you support document creation.
135
00:06:17,210 --> 00:06:20,544
And then finally, the code
you have to put in there is,
136
00:06:20,613 --> 00:06:24,182
when someone wants to open one
of your documents, you have to
137
00:06:24,251 --> 00:06:28,019
write the code to put in MVC
onscreen. And you don't do it
138
00:06:28,088 --> 00:06:31,122
with segueing, so we're gonna
learn a little bit new way to
139
00:06:31,191 --> 00:06:35,694
put an MVC on screen that
does not involve segues. And
140
00:06:35,762 --> 00:06:37,528
the last thing you wanna do
to the template, of course,
141
00:06:37,597 --> 00:06:40,498
is update those types,
the document types to be,
142
00:06:40,567 --> 00:06:44,235
not be imaged, but
to be JSON or emojiart type or
143
00:06:44,304 --> 00:06:46,437
image gallery type for
your homework, or whatever. So
144
00:06:46,506 --> 00:06:49,240
those are the only four things
you do, once you do those four
145
00:06:49,308 --> 00:06:52,076
simple things, you'll get this
full UI. So steps one and
146
00:06:52,145 --> 00:06:55,313
two, you've probably
already done in your app.
147
00:06:55,381 --> 00:06:58,082
Step three looks like this,
this is viewDidLoad of
148
00:06:58,151 --> 00:07:01,519
the UIDocumentBrowser
ViewController's subclass.
149
00:07:01,588 --> 00:07:04,022
And you really have
to the first thing,
150
00:07:04,091 --> 00:07:06,291
which is set it as
its own delegate.
151
00:07:06,360 --> 00:07:09,293
Because it actually implements
a lot of these opening
152
00:07:09,362 --> 00:07:12,463
file methods and stuff using
its own delegate methods.
153
00:07:12,532 --> 00:07:15,566
Then you can have some things
like, allowsDocumentCreation =
154
00:07:15,635 --> 00:07:18,403
true. Do you allow people to
create documents in your app,
155
00:07:18,471 --> 00:07:21,639
or are you just a document
viewer? Do you allow multiple
156
00:07:21,708 --> 00:07:23,942
items to be picked, in other
words, can the user go in and
157
00:07:24,010 --> 00:07:27,612
like Shift + click, or
whatever the equivalent is,
158
00:07:27,681 --> 00:07:30,281
with the touch interface,
to choose multiple items and
159
00:07:30,350 --> 00:07:32,450
open them at once?
Like Emoji Art can't do that,
160
00:07:32,519 --> 00:07:34,519
it can only show one
document at a time.
161
00:07:34,587 --> 00:07:37,589
Then you can also tweak the
look of the UI a little bit.
162
00:07:37,658 --> 00:07:39,624
Change the colors,
tint colors,
163
00:07:39,693 --> 00:07:42,360
things like that, to try and
match your document controller
164
00:07:42,428 --> 00:07:44,195
when it comes up to have
the same kinda look.
165
00:07:44,263 --> 00:07:46,932
So that's it, that's how you
configure it in viewDidLoad,
166
00:07:47,000 --> 00:07:48,933
that's mostly the settings
that you can set.
167
00:07:49,001 --> 00:07:52,337
And then you have to provide
this template URL, you do it
168
00:07:52,405 --> 00:07:55,807
kind in a weird way. There's
this method, DocumentBrowser
169
00:07:55,876 --> 00:07:59,077
didRequestDocumentCreationWit-
hHandler. That's the method
170
00:07:59,146 --> 00:08:01,779
that's saying, somebody wants
to create a document of your
171
00:08:01,848 --> 00:08:05,684
type in your app. What do
you got to give me is a URL
172
00:08:05,752 --> 00:08:08,386
to a template, to a blank.
And the way it makes you
173
00:08:08,455 --> 00:08:11,055
do that is it hands you
a little function called
174
00:08:11,124 --> 00:08:13,691
the importHandler.
There it's called,
175
00:08:13,760 --> 00:08:16,928
I guess I called it Handler
to make it shorter.
176
00:08:16,997 --> 00:08:19,164
But it's actually called
importHandler and
177
00:08:19,232 --> 00:08:22,100
it just takes a URL which is
the URL of a blank document.
178
00:08:22,169 --> 00:08:24,969
And then the import mode
which is either copy this or
179
00:08:25,038 --> 00:08:27,905
move it to create a new
document. So, it would be move
180
00:08:27,974 --> 00:08:30,441
if you created this thing
every time this got called.
181
00:08:30,510 --> 00:08:33,144
It's copy if you create it
once and then you hand it
182
00:08:33,213 --> 00:08:35,480
out every time someone
says new document.
183
00:08:35,548 --> 00:08:37,515
And that's it, all you have
to do is call that handler,
184
00:08:37,584 --> 00:08:41,585
super simple, you'll see that
in the demo. Next, you have to
185
00:08:41,654 --> 00:08:44,489
present your document MVC, so
we have to take a little time
186
00:08:44,558 --> 00:08:47,024
out and go to an aside and
learn something different
187
00:08:47,093 --> 00:08:51,596
which is how to put MVC on
screen without segueing. So
188
00:08:51,665 --> 00:08:54,633
how do we do that? It's
quite easy. There's a method
189
00:08:54,701 --> 00:08:57,936
in UIViewController
called present animated.
190
00:08:58,004 --> 00:09:01,472
You send it to yourself, if
you're a ViewController, and
191
00:09:01,541 --> 00:09:04,242
you give it an argument
of another MVC, and it
192
00:09:04,311 --> 00:09:07,545
presents it animated. And it
presents it on screen modally,
193
00:09:07,614 --> 00:09:11,082
which means it takes over
the entire screen. Okay and
194
00:09:11,151 --> 00:09:14,418
just owns the entire screen
until it dismisses itself,
195
00:09:14,487 --> 00:09:17,855
then it's back to you. Which
is exactly what we want here.
196
00:09:17,924 --> 00:09:23,695
So, the trick here though
is how do I get a new MVC
197
00:09:23,763 --> 00:09:25,413
I told you actually earlier
that iOS has a few MVCs like
198
00:09:25,414 --> 00:09:27,064
to present? And
199
00:09:27,133 --> 00:09:29,867
the camera take, picture
taking MVC and somethings.
200
00:09:29,936 --> 00:09:32,537
So it has some, but what if
you have one of your own?
201
00:09:32,605 --> 00:09:35,039
Okay well, it's really easy
actually. You just put it in
202
00:09:35,108 --> 00:09:38,242
your storyboard, it won't
have any segues drawn to it,