-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEXPERT-Building-Mobile-Apps-with-Ionic-2.txt
42999 lines (11173 loc) · 687 KB
/
EXPERT-Building-Mobile-Apps-with-Ionic-2.txt
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
----------------------- Page 1-----------------------
----------------------- Page 2-----------------------
Contents
1 Introduction 10
Welcome! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Updates & Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
New Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
ECMAScript 6 (ES6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
TypeScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Transpiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2 Ionic 2 Basics 26
Lesson 1: Generating an Ionic 2 Application . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Installing Ionic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Generating Your First Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Adding Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Running the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Updating Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Lesson 2: Anatomy of An Ionic 2 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Important Files & Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
The Less Important Stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Lesson 3: Ionic CLI Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1
----------------------- Page 3-----------------------
Lesson 4: Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Common Decorators in Ionic 2 Applications . . . . . . . . . . . . . . . . . . . . . . . . 46
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Lesson 5: Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
What is a Class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Classes in Ionic 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Creating a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Creating a Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Creating a Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Creating a Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Creating an Injectable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Lesson 6: Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
The * Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Ionic 2 Template Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Lesson 7: Styling & Theming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Introduction to Theming in Ionic 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Methods for Theming an Ionic 2 Application . . . . . . . . . . . . . . . . . . . . . . . . 87
Lesson 8: Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Pushing and Popping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Basic Navigation in Ionic 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Passing Data Between Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Navigation Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Tabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Lesson 9: User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Two Way Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Form Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Lesson 10: Saving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
2
----------------------- Page 4-----------------------
Local Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Ionic Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Lesson 11: Fetching Data, Observables and Promises . . . . . . . . . . . . . . . . . . . . . 116
Mapping and Filtering Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Observables and Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Using Http to Fetch Data from a Server . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Fetching Data from your Own Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Lesson 12: Native Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Using Cordova Plugins in Ionic 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
3 Quick Lists 129
Lesson 1: Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
About Quick Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Lesson 2: Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Generate a new application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Create the Required Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Create the Required Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Create the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Add Pages & Services to the App Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Add Required Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Add Required Cordova Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Set up Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Lesson 3: Basic Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
The Home Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
The Checklist Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Lesson 4: Data Models and Observables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Creating a Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Adding an Observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
3
----------------------- Page 5-----------------------
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Lesson 5: Creating Checklists and Checklist Items . . . . . . . . . . . . . . . . . . . . . . . 168
Checklists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Checklist Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Lesson 6: Saving and Loading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Saving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Loading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Lesson 7: Creating an Introduction Slider & Theming . . . . . . . . . . . . . . . . . . . . . . 195
Slider Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Theming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
What next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
4 Giflist 213
Lesson 1: Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
About Giflist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Lesson Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Lesson 2: Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Generate a new application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Create the Required Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Create the Required Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Add Pages & Services to the App Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Add Required Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Add Required Cordova Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Set up Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Lesson 3: The List Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
4
----------------------- Page 6-----------------------
The Reddit Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
The Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
The Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Using an Observable to Control Searching . . . . . . . . . . . . . . . . . . . . . . . . . 239
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Lesson 4: The Reddit API and HTML5 Video . . . . . . . . . . . . . . . . . . . . . . . . . . 245
HTML5 Video Behaviour on iOS and Android . . . . . . . . . . . . . . . . . . . . . . . 245
Fetching Data from Reddit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Playing our GIFs (videos) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Launching Comments in the In App Browser . . . . . . . . . . . . . . . . . . . . . . . . 256
Loading More GIFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Changing Subreddits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Lesson 5: Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Creating the Settings Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Opening the Settings Page as a Modal . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Saving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Lesson 6: Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
What next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
5 Snapaday 279
Lesson 1: Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
About Snapaday . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Lesson Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Lesson 2: Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Generate a new application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Create the Required Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
5
----------------------- Page 7-----------------------
Create the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Add Pages & Services to the App Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Add Required Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Add Required Cordova Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Set up Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Lesson 3: The Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
The Home Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
The Slideshow Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Lesson 4: Taking Photos with the Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Creating a Photo Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Creating a Simple Alert Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Taking a Photo with the Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Moving the Photo to Permanent Storage . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Updating the Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Lesson 5: Saving and Loading Photos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Implementing the Data Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Lesson 6: Creating a Custom Pipe and Flipbook of all Photos . . . . . . . . . . . . . . . . . . 326
Creating a Custom Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Creating a Slideshow of All Photos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Lesson 7: Integrating Local Notifications & Social Sharing . . . . . . . . . . . . . . . . . . . 332
Local Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Social Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Lesson 8: Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
6
----------------------- Page 8-----------------------
What next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
6 Camper Mate 346
Lesson 1: Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Lesson Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Lesson 2: Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Generate a new application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Create the Required Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Create the Required Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Add Pages & Services to the App Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Add Required Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Add Required Cordova Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Set up Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Lesson 3: Creating a Tabs Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Lesson 4: User Input and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Lesson 5: Implementing Google Maps and Geolocation . . . . . . . . . . . . . . . . . . . . . 380
Connectivity Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Google Maps Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Implementing Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Lesson 6: Saving and Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Lesson 7: Reusing Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Lesson 8: Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
What next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
7 Camper Chat 426
Lesson 1: Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Lesson Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Lesson 2: Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Generate a new application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
7
----------------------- Page 9-----------------------
Create the Required Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Create the Required Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Add Pages & Services to the App Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Add Required Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Install PouchDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Add Required Cordova Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Set up Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Lesson 3: Login Page and Sliding Menu Layout . . . . . . . . . . . . . . . . . . . . . . . . . 440
Lesson 4: Using Facebook for Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Setting up a Facebook App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Installing the Facebook Connect Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Setting up Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Lesson 5: Creating Messages & Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Adding Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Lesson 6: Local and Remote Backend with PouchDB and Cloudant . . . . . . . . . . . . . . 473
Creating a Cloudant Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Integrating PouchDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Lesson 7: Styling & Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Basic Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Creating Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
What next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
8 Testing & Debugging 497
Testing & Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Browser Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
iOS Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Android Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Tips & Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
8
----------------------- Page 10-----------------------
Installing your Application with GapDebug . . . . . . . . . . . . . . . . . . . . . . . . . 505
9 Building & Submitting 507
Preparing Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Generate Icons and Splash Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Set the Bundle ID and App Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Set Cordova Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Minify Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Signing iOS Applications on a Mac or PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Signing iOS Applications on a Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Signing iOS Applications on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Signing Android Applications on a Mac or PC . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Signing an Android Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Building for iOS & Android Using PhoneGap Build (without MAC) . . . . . . . . . . . . . . . . 530
Building with PhoneGap Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Submitting to the Apple App Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Creating an App Store Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Uploading the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Submit for Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Submitting to Google Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Creating a Build for Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Submitting Your Application to Google Play . . . . . . . . . . . . . . . . . . . . . . . . 552
Uploading Multiple APKs with Crosswalk . . . . . . . . . . . . . . . . . . . . . . . . . 556
Updating on the App Stores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Thank you! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
9
----------------------- Page 11-----------------------
Chapter 1
Introduction
Welcome!
Hello and welcome to Building Mobile Apps with Ionic 2! This book will teach you everything you need
to know about Ionic 2, from the basics right through to building an application for iOS and Android and
submitting it to app stores.
Peoplewillhavevaryingdegreesofexperiencewhenreadingthisbook,manyofyouwillalreadybefamiliar
withIonic1,somemayhavesomeexperiencewithIonic2,andsomemayhavenoexperiencewitheither.
Whatever your skill level is, it should not matter too much. All of the lessons in this book are thoroughly
explained and make no assumption of experience with Ionic.
This book does not contain an introduction to HTML, CSS, and JavaScript though. You should have a
reasonable amount of experience with these technologies before starting this book. If you need to brush
up on your skills with these technologies I’d recommend taking a look at the following:
• Learn HTML & CSS
• Learn Javascript
This book has many different sections, but there are three distinct areas. We start off with the basics,
we then progress onto some application walkthroughs and then we cover building and submitting
10
----------------------- Page 12-----------------------
applications.
Alloftheexampleapplicationsincludedinthiscoursearecompletelystandalone. Althoughingeneral,the
applications increase in complexity a little bit as you go along, I make no assumption that you have read
the previous walkthroughs and will explain everything thoroughly in each example. If there are concepts
that need to be explained in more than one walkthrough, I have copied information into both rather than
referring to the other walkthrough.
NOTE: If you have purchased a package which includes the video course, I would recommend watching
it before reading the book. It is not required, but it is a basic introductory level course so it is more of a
logical progression to watch it first.
Updates & Errata
Ionic2isstillindevelopment,sothatmeansthatitisstillchanging. Itisreasonablystablenow,somostof
what you read in this book won’t change, but there will still most likely be some changes until the release
version is reached. I will be frequently updating this book to reflect any changes that are made to the
framework, and you will receive these updates for free. Any time I update the book you should receive
an email notification with a new download link.
I’llbekeepingacloseeyeonchangesandmakingsureeverythingworks,butit’sabigbooksoifyouthink
you have found an error please email me and I’ll get an update out as soon as I can.
Conventions Used in This Book
The layout used in this book doesn’t require much explaining, however you should look out for:
> Blocks of text that look like this
As they are actions you have to perform. For example, these blocks of text might tell you to create a file
or make some code change. You will mostly find these in the application walk throughs. This syntax is
11
----------------------- Page 13-----------------------
usefulbecauseithelpsdistinguishbetweencodechangesIwantyoutomaketoyourapplication,andjust
blocks of code that I am showing for demonstration purposes.
NOTE: You will also come across blocks of text like this. These will contain little bits of information that
are related to what you are currently doing.
IMPORTANT: You will also see a few of these. These are important “Gotchas” you should pay careful
attention to.
Ok, enough chat! Let’s get started. Good luck and have fun!
12
----------------------- Page 14-----------------------
Changelog
Version 12 (this version) - updated for RC.3
• Minor bug fixes and improvements
• Fixed issue with playing videos in Giflist
Version 11 - updated for RC.2
• Minor bug fixes and improvements
• Removed Rollup configuration for PouchDB (no longer needed)
• Bundled extra content with book
Version 10 - updated for RC.1
• Minor bug fixes and improvements (no major changes)
• Moved data loading inside of platform.ready() calls
Version 9 - updated for RC.0
• Please see the official changelog for changes
Version 8 - updated for beta.11
• Changed implementation of overlay components
• Updated forms to use formGroup and formControlName
• Reformatted entire book to a more versatile format
• Added a .mobi and .ePub version of the book
• Various typo and formatting fixes
13
----------------------- Page 15-----------------------
Version 7 - updated for beta.10
• Changed code to use <ion-header> and <ion-footer>
• Removed references to *navbar
• SwitchedtoimportingPouchDBinsteadofrequiring,addedanoteoninstallingtypingsforCamper-
Chat
• Formatting improvements
• Other minor typo and bug fixes
Version 6 - updated for beta.9
Version 5 - updated for beta.8
• Replaced @Page and @App with @Component
• Implemented ionicBootstrap
• For more details on changes in beta.8 see the Ionic 2 Changelog
Version 4 - updated for beta.7
• Updated to use TypeScript instead of ES6
• Updated to use @ViewChild instead of getComponent
• Updated syntax to match latest Angular RC
Version 3 - updated for beta.5 / beta.6
• Removed NgZone as it is no longer required
• Removed es6-shim import from app.js files
• Added changelog
• Added table of contents
• Fixed various typos
14
----------------------- Page 16-----------------------
Version 2 - updated for beta.4
Version 1 - Initial release
15
----------------------- Page 17-----------------------
New Concepts
Ionic1wasbuiltontopofAngular1,whichisaframeworkforbuildingcomplexandscaleableJavascript
applications. WhatIonicdoesontopofAngularisthatitprovidesabunchoffunctionalitytomakemaking
mobile apps with Angular easier. Then along came Angular 2 which is the next iteration of the Angular
framework, which comes with a bunch of changes and improvements. In order for Ionic to make use of
Angular 2 a new framework was required on their end as well, which is how Ionic 2 came about. In short,
by using Ionic 2 & Angular 2 we will be able to make apps that perform even better on mobile, adhere to
the latest web standards, are scalable, reusable, modular and so on.
With the introduction of Angular 2, there has been a lot of changes to how you develop an application.
There are massive conceptual changes, and there have also been a few changes to things like template
syntax as well.
In Ionic 2, your templates will look something like this:
<ion-menu [content]="content">
<ion-toolbar>
<ion-title>Pages</ion-title>
</ion-toolbar>
<ion-content>
<ion-list>
<button ion-item *ngFor="let p of pages" (click)="openPage(p)"></button>
</ion-list>
</ion-content>
</ion-menu>
<ion-nav id="nav" [root]="rootPage" #content></ion-nav>
16
----------------------- Page 18-----------------------
which isn’t too different to Ionic 1, and your Javascript will look something like this:
import { Component } from '@angular/core ';
import { Platform } from 'ionic-angular ';
import { HomePage } from './pages/home/home ';
@Component ({
template : `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
rootPage : any = HomePage ;
constructor(platform : Platform) {
platform .ready ().then (() => {
});
}
}
which is very different to Ionic 1. If you’re already familiar with ECMAScript 6 or TypeScript then a lot of
this probably won’t be too hard of a change for you, but if these are completely new concepts to you
(and for most people it will be) the transition might be a little more difficult. To help put your mind at ease
somewhat, ES6 and TypeScript was all completely new to me when the Ionic 2 alpha first came out, and
withinaprettyshorttimeperiod,Istartedtofeelverycomfortablewithit. NowIamwaymorecomfortable
with the new syntax and structure than I ever was with Ionic 1.
In this lesson we are going to broadly cover some of the new concepts and syntax in Ionic 2 & Angular 2.
The intention is just to give you a bit of a background, we will get into specifics later.
17
----------------------- Page 19-----------------------
ECMAScript 6 (ES6)
Before we talk about ECMAScript 6, we should probably talk about what ECMAScript even is. There’s
quite a bit of history involved which we won’t dive into, but for the most part: ECMAScript is a standard,
Javascript is an implementation of that standard. ECMAScript defines the standard and browsers imple-
ment it. In a similar way, HTML specifications (most recently HTML5) are defined by the organising body
and are implemented by the browser vendors. Different browsers implement specifications in different
ways, and there are varying amounts of support for different features, which is why some things work
differently in different browsers.
The HTML5 specification was a bit of a game changer, and in a similar way so is the ECMAScript 6 spec-
ification. It will bring about some pretty drastic changes to the way you will code with JavaScript and in
general, will make Javascript a much more mature language that is capable of more easily creating large
and complex applications (which JavaScript was never really originally intended to do).
We’re not going to go too much into ES6 here, because you will learn what you need to know throughout
the book, but I will give a few examples to give you a sense of what it actually is. Some features ES6
introduced to Javascript are:
Classes
class Shape {
constructor (id, x , y) {
this .id = id
this .move (x , y)
}
move (x , y) {
this .x = x
this .y = y
}
}
18
----------------------- Page 20-----------------------
This is a big one, and something you would be familiar with if you have experience with more traditional
programming languages like Java and C#. People have been using class-like structures in Javascript for
alongtimethroughtheuseoffunctions,buttherehasneverbeenawaytocreatearealclass. Nowthere
is. If you don’t know what a class is, don’t worry, there is an entire lesson dedicated to it later.
Modules
// lib/math .js
export function sum (x , y) { return x + y }
export var pi = 3.141593
// someApp.js
import * as math from "lib/math "
console .log("2PI = " + math .sum(math .pi , math .pi))
// otherApp.js
import { sum, pi } from "lib/math "
console .log("2PI = " + sum(pi , pi))
Modules allow you to modularise your code into packages that can be imported anywhere you need in
yourapplication,thisissomethingthatisgoingtobeheavilyusedinIonic. Wewillgetintothismorelater,
but essentially any components we create in our application we “export” so that we can “import” them
elsewhere.
Promises
Promises are something that have been made available by services like ngCordova previously, but now
they are natively supported, meaning you can do something like this:
doSomething ().then ((response) => {
console .log(response);
});
19
----------------------- Page 21-----------------------
Block Scoping
Currently,ifyoudefineavariableinJavascriptitisavailableanywherewithinthefunctionthatitwasdefined
in. ThenewblockscopingfeaturesinES6allowyoutousethe let keywordtodefineavariableonlywithin
a single block of code like this:
for (let i = 0; i < a .length ; i++) {
let x = a [i];
}
If I were to try and access the x variable outside of the for loop, it would not be defined.
Fat Arrow Functions
One of my favourite new additions is fat arrow functions, which allow you to do something like this:
someFunction((response) => {
console .log(response);
});
rather than:
someFunction(function (response){
console .log(response);
});
At a glance, it might not seem all that great, but what this allows you to do is maintain the parent’s scope.
In the top example if I were to access the this keyword it would reference the parent, but in the bottom
example I would need to do something like:
var me = this ;
someFunction(function (response){
console .log(me .someVariable);
20
----------------------- Page 22-----------------------