34,79 €
In this book, you will work through seven different projects to get you hands-on with developing amazing applications for iOS devices.
We start off with a project that teaches you how to build a utility app using Swift. Moving on, we cover the concepts behind developing an entertainment or social networking related application, for example, a small application that helps you to share images, audio, and video files from one device to another. You’ll also be guided through create a city information app with customized table views, a reminder app for the Apple Watch, and a game app using SpriteKit.
By the end of this book, you will have the required skillset to develop various types of iOS applications with Swift that can run on different iOS devices. You will also be well versed with complex techniques that can be used to enhance the performance of your applications.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 259
Veröffentlichungsjahr: 2015
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: October 2015
Production reference: 1211015
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-076-5
www.packtpub.com
Author
Cecil Costa
Reviewers
Eugene Mozharovsky
Alexey Smirnov
Jak Tiano
Commissioning Editor
Dipika Gaonkar
Acquisition Editors
Larissa Pinto
Sam Wood
Content Development Editor
Zeeyan Pinheiro
Technical Editor
Shivani Kiran Mistry
Copy Editor
Akshata Lobo
Project Coordinator
Suzanne Coutinho
Proofreader
Safis Editing
Indexer
Monica Ajmera Mehta
Graphics
Disha Haria
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
Cecil Costa, also known as Eduardo Campos in Latin countries, is a Euro-Brazilian freelance developer who has been learning about computers since he got his first PC (an AT 286) in 1990. From then on, he kept learning about programming languages, computer architecture, and computer science theory.
Learning and teaching are his passions; this is the reason why he worked as a trainer and an author. He has been giving on-site courses for companies such as Ericsson, Roche, TVE (a Spanish television channel), and a lot of other companies. He is also the author of Swift Cookbook and soon he will also write a book called Reactive Swift Programming.
Nowadays, he teaches through online platforms, helping people from every part of the world.
In 2008, he founded his own company, Conglomo Limited (http://www.conglomo.es/), which offers development and training programs both on site and online.
Over his professional career, he has created projects by himself and also worked for different companies, from small to big ones, such as IBM, Qualcomm, Spanish Lottery, and DIA%.
He develops a variety of computer languages (such as Swift, C++, Java, Objective-C, JavaScript, Python, and so on) in different environments (iOS, Android, Web, Mac OS X, Linux, Unity, and so on) because he thinks that a good developer needs to learn every kind of programming language to open his mind, and only then will he really know what development is.
Nowadays, Cecil is based in the UK, where he is progressing in his professional career, working with augmented reality on mobile platforms.
I would like to thank Mr Robert William Bemer for creating the escape key and my son Gabriel Campos Oliveira for bringing happiness to my life.
Eugene Mozharovsky started his computer science journey in 2010 with a school course on programming in Pascal. Then, he explored Java for himself and it was the whole world of object-oriented programming, a full-featured API, and powerful client-server techniques. In 2013, he switched to Mac OS and found his true passion in app development for Apple mobile devices. In summer 2014, he fell in love with Swift and iOS 8 beta and is currently working on a handy social app. When he doesn't code, he tries to systematize physics for his own understanding of how the Universe works or train his exotic parrots.
Alexey Smirnov works as a software engineer at a small start up company called iRONYUN (http://ironyun.com). In his spare time, he enjoys building iOS apps using Objective-C and Swift. Alexey got his master's degree in computer science from Stony Brook University, USA.
Jak Tiano is a mobile designer and programmer. He specializes in mobile game development using Unity3D, but also works with countless other engines. He has been developing iOS applications since 2008, and has been all-in on Swift since its release in 2014. He works as a freelance mobile developer, and co-runs not a hipster coffee shop, an independent game studio in Burlington, VT.
I'd like to thank the great people at Packt for giving me the opportunity to contribute to this book and Taylor Swift for frequently providing the background music for my review sessions.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Swift, a relatively new computer language created by Apple Computers and its version 2 was already released. This programming language is gradually gaining features, performance, and stability. This book shows you how to create different apps using different frameworks. Thus, after reading this book, you will have a big skill set for Swift development.
Chapter 1, Exploring Xcode, explores some features of Xcode. It gives you some tips on how to debug and develop more quickly.
Chapter 2, Creating a City Information App with Customized Table Views, shows you how to create an app with different scenes and table views, retrieving information from the Internet. Here, you will learn how to use SwiftyJSON, a framework that allows you to work with JSON messages very easily.
Chapter 3, Creating a Photo Sharing App, will show you how to use the camera, edit your photo, and share it with your friends using the social framework.
Chapter 4, Simulating Home Automation with HomeKit, will show you how to simulate a house with its devices, create an app that retrieves your devices' information, and also change their state. This kind of app will be popular very soon due to the popularity of the Internet of Things (IoT).
Chapter 5, Health Analyzing App Using HealthKit, will teach you how to use HealthKit. Here, you are going to do some queries to receive and update the user's health data. You will appreciate a different way of treating the data as it needs to be converted into your favorite unit. Besides this, you will learn a third-party framework called iOS Chart. Here, you will be able to display some charts to the user to check their progress.
Chapter 6, Creating a Game App Using SpriteKit, is a chapter for those who like playing games on their phones. It is even more fun when you learn how to create your game, mainly if it is with SpriteKit, a framework that is very easy to follow and made for developing 2D games. We will develop a game based on a surfer dinosaur that needs to dodge the enemies. To move the character, we will use the accelerometer sensor.
Chapter 7, Creating an Apple Watch App, shows us how to create an app that controls our fridge. We can check our Apple Watch for the amount of food that we still have. This app also helps us display the route on the map to the supermarket.
Chapter 8, AVFoundation, shows how your phone, besides recording videos, can also edit them. Here, we are going to use a low-level framework called AVFoundation to change the audio of an existing video from the photos gallery. To do this app, we will also need some help from the photos framework.
As you are developing iOS apps, you will need a relatively new Apple computer with OS X Yosemite (10.10) or above, Xcode 6 or above, and for some chapters, a physical Apple mobile device would be needed, because some features are not supported by the simulator. A few cases will require you to be enrolled in the Apple Developer Program due to the requirement of some capabilities. An Internet connection is also required for some chapters.
You! That's right, if you are an iOS developer and you want to do some real-life examples using the Swift programming language. If you are willing to learn a big variety of iOS frameworks, this is the right book for you, as we are going to develop seven apps using different frameworks.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Swift_2_Blueprints_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
Programming is not only about code, it is also about methodology. It doesn't matter how many years you've been programming with Xcode, there is always a new feature that can speed up your development, mainly nowadays that there is a new version every few months. Don't forget that Swift is a new language created to replace the old Objective-C, which means that Xcode also needs to adopt new features for this new programming language.
This book is about creating applications with the Swift programming language using Xcode 6 as an IDE. The idea behind these apps is to show how to create different kinds of real apps from scratch and this chapter presents with you some tricks on how to use Xcode.
Even if you are already a developer with years of experience in Xcode, it is worth reading this chapter because there is always a different way to do a task and it can be very helpful. So, let's start reviewing some Xcode and Swift features. In this chapter, we will cover:
Have you ever thought of how much time a developer expends in moving the mouse pointer? It can be a lot of time. How about reducing some time by memorizing a few key combinations. Of course, don't expect to memorize all of them in a day. You can practice them when it is necessary; you will see that, after a while, you will save a lot of time. Of course, command + X, command + C, command + V, and command + Q are not going to be mentioned for they are assumed to be known.
The first shortcut we are going to learn is command + B, which is to build the solution without running it on the device or simulator. This key combination is very useful when you want to check whether the project has any errors, but you don't want to waste time installing the app.
Sometimes, mainly when you have Swift and C on the same project, the compiler caches the object files wrongly, so the best solution would be to clean everything up and recompile again. To clean your entire project, use the command + shift + K combination. Cleaning is a fast process, nevertheless, you have to remember that afterward you need to rebuild your project, which might take a while.
If you want to build your product and run it, you have two options. The first is command + R that compiles your project if it is necessary and installs it on the device or simulator; this combination is equivalent to pressing play on the left-hand side of the toolbar. The second option is control + command + R, which installs the last build but doesn't rebuild the project; it is very handy when your project takes a long time to compile and you just want to reinstall it again for testing.
Now, let's learn some key combinations that will affect Xcode, visually speaking. On the left-hand side, we have the Navigator. As you know, here is where you can access the project files, the search results, and the compilation status. The following screenshot shows a sample of the Navigator:
If you need more visual space, you can hide the Navigator area with command + 0, or you can show this area using the same combination. It is very useful when you have a small screen like a MacBook screen and you need to work with the interface builder or Playground.
As you can see, there is a bar on the top (called the Navigator bar), which allows you to access different sections of the Navigator. You can click on each icon or you can save some time by pressing command + a number from 1 to 8, 1 being the project navigator (folder icon), 2 the symbol navigator, and so on till 8, which is the report navigator.
Every Navigator section has a text field at the bottom to filter the content that is being displayed on the Navigator. You can reach this text field very fast by using the command + option + J combination. So, based on the previous combination, when you need to access a file, you can go to the project navigator by pressing command + 1 followed by command + option + J.
To finish with the Navigator area, you should know that you have a shortcut to go to the project navigator and highlight the current file, it is command + shift + J.
The area on the right-hand side is called the Utility area and its combinations are similar to the Navigator area. This area can be hidden or shown with command + option + 0, its sections can be accessed with command + option + a number from 1 to 6, and its filter can be reached with command + option + L. The following screenshot is a sample of the Utility area:
The area located between the Navigators and the Utility area at the bottom of the Xcode screen is called the Debug area. This area can be shown or hidden with command + shift + Y. Here, you have some debug combinations like command + Y to enable or disable breakpoints or pause or resume the application with control + command + Y. There are more debugging keyboard shortcuts; as they require the use of fn keys (F6, F7, F8), it can be complicated according to your keyboard. Here, you have a sample of the Debug area:
Now, the central area, which is the most important one, is called the Editor area. Here, as you know, is where you type your code. Scrolling up and down could waste a lot of time, so let's learn some shortcuts that will make us find our code faster. The first combination is command + F that opens a text field to search in the current file.
When you need to search in the whole project, you can use the command + shift + F combination, which is much faster than a click on the loupe icon and a click on the text field. Another similar combination is command + shift + O (letter O, not the number zero), which is called Open Quickly…. This combination opens a text field in front of your editor area and allows you to search for symbols. It also allows you to type the first letter of each symbol word, like the following example in which NSHD was typed and it was able to find NSHomeDirectory.
Open Quickly searches for symbols in the current project and also inside the frameworks. But if you like to jump to a symbol (method, property, global variable, and so on), you can use control + 6 as it opens the combo box that is located at the top of the editor area like in the following screenshot:
To navigate through the files you've been using, you can use control + command + ← to go to the previous files or control + command + → for the next one. Alternatively, you can slide with two fingers on use the touch pad if you have one.
When you have a code that is quite difficult to understand because it is not well-formatted, you can use the control + I combination. If you have selected some lines, only these lines will be arranged. If no line is selected, then the current line of code will be formatted.
To finish this shortcuts section, it is worth mentioning that you have command + \ to toggle breakpoints and command + / to toggle the selected line comments.
Don't worry if you think that there are too many combinations to memorize or if some of them are hard to do, you can always check the combination and customize it if you want by opening the Xcode menu, selecting preferences, and opening the Key Bindings section like in the following screenshot:
Bear in mind while customizing your shortcuts that when you work on another computer, you will have to set them up again.
Every project should have a version control system, even if you are the only developer. Xcode works with Git as a default VCS, which has a special way of working.
Firstly, you have to know that Xcode offers you just the basic usage of Git; there are times when you might need to use the command line.
You can use Git in two ways: locally only or remote and locally. The first one is usually done by single developers and can be done very easily by checking this option when you have to select a folder for your new project. The following screenshot shows the option that needs to be checked in the file dialog:
The second way of working with Git is to use it remotely and locally. What does it mean? It means that you can create versions of your code locally and when you think that it is ready to be shared with the other members of the team, you can send it to the server. To do so, you have to configure a server by yourself or you can use this service from a third-party company, such as GitHub or BitBucket.
Recently, Apple announced the Xcode server, where you can configure your Git server and also a continuous integration system. Explaining how to configure this server can be very exhausting and is out of the scope of this book. However, if you would like to create your own server, you can have a look at this possibility.
As a developer what you need to work with a remote repository is its URL. You have to request it to the system administrator or you can copy it from the website where you created it. GitHub, for example, shows it on the right-hand side with a button to copy the URL on the clipboard, as you can see in the following screenshot:
Once you have the URL, you can check it out by selecting the Check out an existing project option on the start up screen, as shown in the following screenshot. Or, you can click on the Source Control menu and select the Check out... option.
Once you have your project checked out and have started working on it, it doesn't matter whether it was created from a server or locally only. You will notice that when a file is modified, letter M will appear next to your file name on the project navigator. Letter A will also appear when a new file is added to the project. The next screenshot shows a sample of some files that were added or modified:
Note that letter M can also appear next to your project; it means that something in the project.pbxproj file, which is inside of your project package. This file contains the information of your project like the source code files with their path and the settings; hence, you have to deliver it every time you add a file or change any setting.
To commit the modified and new files, you just need to use the command + option + C key combination. A dialog asking for a description of the modifications will appear, leave a message in a way that another developer understands what was done.
Avoid committing without leaving a comment, or just leaving a nonexpressive comment like "modifications." Comments are very useful to check the project's progress and fix bugs.
These commits are done locally by default. It means that only your computer knows about your modifications. If you want to send your commits to the server, don't forget to check the option that is under the comment box, as it is demonstrated in the next screenshot:
The other way around, which means receiving updates from the server, is called pull. To do a pull from the server, you just need to press the command + option + X combination key.
