Table Of ContentBOOKS FOR PROFESSIONALS BY PROFESSIONALS® Companion
eBook
Available
Make the jump to iOS development using
In 250 pages, Migrating to Swift from Android gives you—as an experienced
Apple’s Swift programming language
Android app developer—all you need to create native iOS apps using the
latest Swift programming language. Starting with preparing your Xcode 6 IDE
and introducing just enough iOS application framework fundamentals, you’ll
understand how to create meaningful Swift applications for iOS 8 immediately.
M
After the short IDE setup guide, this book continues by providing guidance on
how to translate your existing Android apps to iOS. You’ll plan and structure
i
your iOS apps following a typical top-down process using Xcode Storyboards. g
To implement use cases, you’ll cover common mobile topics, including user
r
interfaces, managing data, and networking with remote services. As you move a
through Part 2 of this book, you’ll create simple and meaningful iOS apps with
t
rich UI components to handle common CRUD operations locally and remotely.
i
n
Part 3 demonstrates the whole porting process by translating a typical mobile
app from Android to iOS 8 from start to finish. When you finish reading g
Migrating to Swift from Android, you’ll be an iOS developer as well as an
t
Android developer. In most cases, it is a waste not to port your native Android o
apps to iOS.
S
w
i
f
t
f
r
o
m
Migrating
A to
n
d
r
Swift Android
o
i from
d
Sean Liao
L
i
a
o
COMPANION eBOOK
ISBN 978-1-4842-0437-5
52999
Shelve in
Mobile Computing
SOURCE CODE ONLINE
User level:
www.apress.com Beginning 9781484204375
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a
Glance
About the Author ������������������������������������������������������������������������������ix
About the Technical Reviewer ����������������������������������������������������������xi
Acknowledgments ��������������������������������������������������������������������������xiii
Introduction �������������������������������������������������������������������������������������xv
■ Part I : Prepare Your Tools ���������������������������������������������� 1
■ Chapter 1: Setting Up the Development Environment ��������������������3
■ Chapter 2: iOS Programming Basics ��������������������������������������������11
■ Part II: A Roadmap for Porting ������������������������������������� 45
■ Chapter 3: Structure Your App ������������������������������������������������������47
■ Chapter 4: Implement Piece by Piece �����������������������������������������117
■ Part III: Pulling It All Together ������������������������������������ 185
■ Chapter 5: Recap with a Case Study ������������������������������������������187
Index ����������������������������������������������������������������������������������������������245
iii
Introduction
In 2000, I started my first PalmOS mobile app for an inventory-tracking project.
The initial project was a full-staffed team effort that consisted of mobile
developers, SAP consultants, supply-chain subject matter experts, middleware
developers, QA testers, architects, business sponsors, and so forth. JavaME
came up strong in 2002, followed by Pocket PC/Windows Mobile. I did several
mobile projects in which I converted mobile apps to the Pocket PC platform by
blindly translating JavaME code to C# .NETCF mobile code. These “translation”
efforts prolonged the whole product life cycle. The project achieved higher ROI
as the product life extended, because the extra cost of translating mobile code
was surprisingly low. Ever since then, I have been translating front-end mobile
apps among JavaME, BlackBerry, and Windows Mobile platforms.
In early 2009, by repeating the same porting process, I created my first iOS
app by translating a Windows mobile app. That started my iOS programming
journey, and later it was a no-brainer for me to try porting to Android. Most
mobile apps are platform agnostic. When you have the whole solution
completed for either iOS or Android, all the business and technical issues
have been verified and the other deliverables and project artifacts are already
reusable. It would be a waste not to port it to the other platform. Earlier this
year, I wrote my first book, Migrating to Android for iOS Developers, to share my
beliefs and experiences. After Swift was introduced at Worldwide Developers
Conference (WWDC) 2014, I decided to write this book because the similarity
between Java and Swift makes the porting method even easier while the same
ROI analysis remains true.
The primary objective of this book is to help experienced Android developers
leap into native iOS–Swift mobile development. It is easier than you think,
and this book will make it even easier with Android analogies and mapping
guidelines. You can immediately translate common mobile use cases to iOS.
xv
xvi Introduction
Who Is This Book For?
This book is specifically written for Android developers who want to take
advantage of their mobile knowledge and make mobile applications available
on the iOS mobile platform. The book will show you the common iOS
programming subjects and frameworks using your familiar Android vocabularies
without lengthy explanations, because you already know these mobile subjects
from being an Android developer.
How This Book Is Organized
In Part I, you will get the iOS Xcode integrated development environment (IDE)
up and running in no time. You will be guided in creating tutorial projects that
will become your porting sample projects. I believe this is the best way for you
to get hands-on experience while learning programming topics.
Part II of this book shows you how to plan and structure your iOS apps: by
creating a storyboard and breaking the app into model-view-controller (MVC)
classes. You will be able to reuse most of the existing software artifacts and
design the rest from their Android counterparts. The common mobile topics are
followed, including user interface, managing data, and networking with remote
services. After you finish Part II, you will be able to create simple but meaningful
iOS apps with rich UI components, and to handle common CRUD (create, read,
update, delete) operations locally and remotely.
Last, Part III walks you through a case study that ports a complete iOS app to
Android. It recaps how to use mapping guidelines from the topics in Part II. You
can also use the book’s table of contents to help find the porting guidelines as
needed.
When you complete this journey, you will be able to use Xcode and Swift to
effectively port your existing Android apps to iOS.
I
Part
Prepare Your Tools
A handy tool makes a handy person. This is very true for creating software,
too. Xcode is the integrated development environment (IDE) for writing,
compiling, debugging, and building code for iOS application development.
The first part of the chapter walks you through the installation and steps for
getting it up and running. All the topics in this book come with sample code.
You will need to use Xcode to learn from these sample projects, and you will
use Xcode to create world-class iOS apps, too.
For iOS programming, Swift is the latest and greatest programming
language released for iOS 8. The chapters in this part will give you enough
knowledge to read and write the sample code in Swift. You will find learning
Swift a very natural extension from your use of Java, and you will surely feel
comfortable using the code from this book as your own code.
1
1
Chapter
Setting Up the
Development
Environment
It is more fun to see apps run than to read the source code, and you cannot
get hands-on programming experience by just reading books. Let’s get the
development environment up and running first so you can use it—and learn
Swift programming for iOS along the way.
Xcode and the iOS SDK
ANDROID ANALOGY
The Android Developer Tools (ADT plug-in for Eclipse or Android Studio.
Xcode is the complete toolset for building iOS apps. It is an integrated
development environment (IDE) that helps you build, test, debug, and
package your iOS apps. It is free but you must have an Intel-based Mac
running Mac OS X Mavericks or later. You will use the latest Xcode,
version 6, throughout this book.
3
4 CHAPTER 1: Setting Up the Development Environment
Installing from the Mac App Store
Xcode is distributed in the Mac App Store, which takes care of the download
and install for you. With a single click to start the download and installation
of Xcode, you get the compilers, code editor, iOS SDK, debugger, device
emulators, and everything you need to create iOS apps. Figure 1-1 shows
Xcode in the Mac App Store app.
Figure 1-1. Xcode in Mac App Store
All you need to do is install the latest Xcode from the Mac App Store.
After completing the installation, go ahead and launch Xcode from the
Applications folder. Keep it in the Mac OS Dock so that you can launch it at
any time.
The first time you launch Xcode, it immediately prompts you to install the
required components (see Figure 1-2). Click Install to complete the Xcode
installation.
CHAPTER 1: Setting Up the Development Environment 5
Figure 1-2. Install the required components
After the required components are installed, you should see the screenshot
in Figure 1-3. Your iOS IDE, Xcode, is ready!
Figure 1-3. Welcome to Xcode
Create an iOS Project Using the Template
ANDROID ANALOGY
New Android Application Project template in ADT.
You’ve got the right tool; now, wouldn’t you like to see some real
action—like creating an iOS app and seeing it run? I’d like that, too!
You want to do this to ensure your IDE is working properly as well.
6 CHAPTER 1: Setting Up the Development Environment
I actually created my very first Android app using the ADT New Android
Application Project template when I had no idea how to create Android
mobile apps. All I wanted was to see something running in no time. Yep,
ADT did it for me nicely. I was very happy with myself when I felt I’d created
an Android app without knowing anything! Hey, there’s nothing wrong with
making yourself happy, right?
Xcode offers the same thing. The objective of this section is to show you
how to create an iOS app as quickly as possible. Hold any programming
questions so you can finish the project as fast as you can. For now,
complete the following steps:
1. Launch Xcode if you haven’t launched it yet.
2. Select Create a new Xcode project from the Welcome
to Xcode screen (see Figure 1-3). Figure 1-4 shows
the prompt that asks you to choose a template for your
project:
a. In the left panel of Figure 1-4, select
iOS ➤ Application.
b. In the right panel of Figure 1-4, you may choose any
of the templates. Just for fun, choose Game.
c. Click the Next button.
Figure 1-4. Choose a template
Description:In 250 pages, Migrating to Swift from Android gives you—as an experienced Android app developer—all you need to create native iOS apps using the latest Swift programming language. Starting with preparing your Xcode 6.1 IDE and introducing just enough iOS application framework fundamentals, you'l