Saturday, August 17, 2013

Getting Started with Mobility, Part 2: Native iOS Development

In this series, we're introducing mobile development by looking at a variety of platforms. In Part 1, we provided an overview of the mobile landscape, and discussed various types of client app development (native, hybrid, mobile web) and supporting back-end services. Here in Part 2, we'll introduce you to native development for iOS (iPhone and iPad).

iOS, The Innovator
Apple bills iOS as "the world's most advanced mobile operating system." iOS and the iPhone occupy a special place in the history of mobile devices, having set the standard for what a smartphone should be--and heavily inspiring the design choices of the competition. The iPad similarly caused a revolution, establishing the smart touch tablet as an essential device. Although facing heavy competition from Google's Android, the iPhone and iPad are considered by many to be the premium mobile devices available today. Apple provides both the iOS operating system and the iPhone/iPad hardware, which puts them in full control of the experience.



The Computer You'll Develop on: A Mac
For native iOS development, you need a Mac, period.

Developer Tools: XCode
XCode is the Apple development environment, and it gives you everything you need for iOS development including an iPhone/iPad simulator. You don't need to sign up as an Apple developer to develop iOS apps and run them in the simulator, but you do need to join up (currently $99/yr) in order to run them on an actual device.

There's a lot in XCode: it gives you a very complete environment that includes a code editor, Interface Builder (a user interface editor), and various wizards / assistants / dialogs for setting properties, wiring up events, and so on. However, expect that it's going to take you some time to really learn your way around it.

XCode Development Environment

Programming: Objective-C and Cocoa Touch
You'll be programming in a language named Objective C and working with Cocoa Touch--a family of frameworks for iOS development. Cocoa Touch provides services such as user interface, animation, media, and data management.

As its name implies, Objective-C is yet another object-oriented, C-dervived programming language; however, it comes from the SmallTalk school of object orientation which means there are differences from what you may be used to if your prior experience is with C++, C#, or Java. Here's an example of Objective-C code to give you idea of what it looks like:

#import "BirdSightingDataController.h"
#import "BirdSighting.h"

@interface BirdSightingDataController()
- (void)initializeDefaultDataList;
@end

@implementation BirdSightingDataController
- (void)initializeDefaultDataList {
    NSMutableArray *sightingList = [[NSMutableArray alloc] init];
    self.masterBirdSightingList = sightingList;
    BirdSighting *sighting;
    NSDate *today = [NSDate date];
    sighting = [[BirdSighting alloc] initWithName:@"Pigeon" location:@"Everywhere" date:today];
    [self addBirdSightingWithSighting:sighting];
}

- (void)setMasterBirdSightingList:(NSMutableArray *)newList {
    if (_masterBirdSightingList != newList) {
        _masterBirdSightingList = [newList mutableCopy];
    }
}

- (id)init {
    if (self = [super init]) {
        [self initializeDefaultDataList];
        return self;
    }
    return nil;
}

- (NSUInteger)countOfList {
    return [self.masterBirdSightingList count];
}

- (BirdSighting *)objectInListAtIndex:(NSUInteger)theIndex {
    return [self.masterBirdSightingList objectAtIndex:theIndex];
}

- (void)addBirdSightingWithSighting:(BirdSighting *)sighting {
    [self.masterBirdSightingList addObject:sighting];
}
@end

Some things about Objective C that initially trip up new developers include the following:

  • An interface is what you may be used to calling a class.
  • A protocol is what you may be used to calling an interface.
  • You refer to a current object as self (this in many other languages).
  • You use nil for no value (null in many other languages).
  • You use the constants YES and NO for 1 and 0 (true and false in many other languages).
  • All methods (functions) are public.
  • Methods defined with a leading minus sign (-) are instance methods.
  • Methods defined with a leading plus sign (+) are class (static) methods.
  • Square brackets indicate message passing, which is how you invoke a method on an object rather than the dot notation you encounter in many other languages.
  • You have to be careful about memory management, you don't have garbage collection like you do in C# and Java. You'll want to take advantage of a feature called Automatic Reference Counting (ARC) to simplify your memory management obligations.
  • There's an at-sign (@) statement syntax for declaring things like interfaces and implementations, reflecting Objective-C's original roots as a C preprocessor.
  • In an interface or implementation, properties and variables go within a block of curly braces, while methods go outside it.
  • You need to keep declarations (in a .h file) separate from your implementation (.m file), as in C++.
Like anything else, once you get used to Objective-C you can be effective in it.

Testing Your Apps: The iOS Simulator
The iOS Simulator allows you to easily test your iPhone and iPad apps right on your Mac. Command-R out of XCode invokes the simulator.
iOS Simulator

Online Resources
There are many good tutorials and reference materials to be found at developer.apple.com. Start here, at Start Developing iOS Apps Today.

Apple Developer Center
 
Also be sure to check out these online references:
iOS Technology Overview
User Experience Starting Point for iOS
iOS Human Interface Guidelines
Write Objective-C Code
Core Data Starting Point
Data Management Starting Point
Graphics and Animation Starting Point
Audio and Video Starting Point
Networking and Internet Starting Point
Performance Starting Point for iOS
Sample Code

Tutorials
You can find many different suggestions online for which books, courses, and sites are most useful for learning iOS.

1. Ray Wenderlich iOS Tutorials

Even if you're an experienced developer, you may find Objective C to be quite a hurdle at first. For that reason, I suggest swallowing your pride a bit and starting with some very basic tutorials that lead you by the hand, step-by-step. Personally, I've found the tutorials at http://www.raywenderlich.com to be well-written and a good way to learn the basics quickly while building apps.

Learn to Code iOS Apps 1: Welcome to Programming
Learn to Code iOS Apps 2: Strings, Arrays, Objects and Classes
Learn to Code iOS Apps 3: Your First App
Learn to Code iOS Apps 4: Making it Beautiful

By the time you make your way through these first four tutorials, you'll have created a game with audio.
Result of Learn to Code iOS Apps 4 Tutorial

There are plenty of intermediate and advanced tutorials also available from the same site.


2. Apple iOS Tutorials

I've also found the tutorials at developer.apple.com to be very good. Here are links to the first three:

Your First iOS App

In this tutorial you receive an introduction to Xcode, and develop a simple app with a text box, label, and button. This is meant to have you walk before you run, doesn't assume much in the way of prior knowledge, and only takes 1-2 hours. It will give you a good grounding in learning your way around XCode.



Your Second iOS App: Storyboards

In this tutorial you develop a birdwatching app with master-detail views and multiple storyboard scenes. You'll be spending a lot of time with the storyboard editor as well as writing code. This is a much more involved tutorial than the first one. Here, you create an app using a master-slave project template, define a new data model, and create multiple scenes (master, detail, add), linked with segues. In this tutorial you're getting an excellent grounding in things you will need to do frequently in real iOS apps. It's well worth the time. Time: 4-8 hours. 



Your Third iOS App: iCloud

In this tutorial you develop an app that creates and manages text files in Apple's iCloud storage. Unlike the previous two tutorials, this one doesn't spell every little step out for you and is also slightly out of date, so it may challenge you a bit; if you run into trouble with it, refer to the complete code listings at the end to get your project right. Unlike the previous labs, you'll be registering your app with the Apple developer center and will run it on an actual iPhone rather than in the simulator. Time: 4-8 hours.
There are plenty of other good tutorials online beyond these. At some point, you'll move behind prepared tutorials and develop your own apps--perhaps beginning with making your own variations to the tutorials. There's lots of good forum support online so you won't be alone in your journey.

Summary: Native iOS Development
It definitely takes a substantial investment to become a proficient native iOS developer: it won't come overnight, and it may feel like you're making slow progress at first--but stay with it, and you'll be amply rewarded. Given the importance of the iOS platform, it's a wise career move to develop iPhone and iPad programming skills.

Next: Getting Start with Mobility, Part 3: Native Android Development

No comments: