Showing posts with label iOS. Show all posts
Showing posts with label iOS. Show all posts

Tuesday, August 27, 2013

Getting Started with Mobility, Part 7: Hybrid Development in JavaScript with Titanium

In this series of posts, we're looking at how to get started as a mobile developer. 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. In Parts 2-5, we examined native app development for iOS, Android, Windows Phone, and Windows 8. In Part 6, we begin examining hybrid development, starting with C# using Xamarin. And now, in Part 7, we'll look at JavaScript mobile development using Titanium.



Like Xamarin, Titanium is a hybrid solution that facilitates use of a familiar language and generates native apps. We made the point in Part 6 that any hybrid solution you're considering should be carefully examined to assess cost, performance, reliability, and risk.


Titanium: Mobile Development in JavaScript
Appcelerator Titanium is a leading hybrid mobile platform. JavaScript is arguably the most popular programming language on planet earth today--anyone who does web development has had to learn it, and the language hasn't stopped there: thanks to node.js, it is also being used server-side. It makes sense, therefore, to make mobile app development accessible to the world's large numbers of JavaScript developers. That's what Titanium does. To quote the Titanium web site, Titanium is "An open, extensible development environment for creating beautiful native apps across different mobile devices and OSs including iOS, Android, Windows and BlackBerry, as well as hybrid and HTML5. It includes an open source SDK with over 5,000 device and mobile operating system APIs, Studio, a power Eclipse-based IDE, Alloy, an MVC framework and Cloud Services for a ready-to-use mobile backend."

Titanium generates native apps that execute a JavaScript interpreter at runtime, but a very efficient one, paired with native objects in the mobile platform's runtime environment (see What Titanium Appcelerator Really is and How it Works). This is different from executing within a browser, the approach taken by PhoneGap and Icenium.

Titanium development can be fee or paid, depending on how much of Appcelerator's services you are interested in taking advantage of. From the web site: "The free version of Titanium is primarily a solution for the development of mobile apps, whereas the Appcelerator Platform supports the entire mobile lifecycle, along with commercial support, SLAs and technical training. The Appclerator Platform includes Appcelerator Studio, Appcelerator Alloy, Appcelerator Cloud, Appcelerator Test, Appcelerator Analytics, and Appcelerator Performance Management." (please review Plans and pricing and this forum thread).


The Computer You'll Develop On: Windows, Mac, or Linux
Titanium's IDE, Titanium Studio, is available in Windows, Mac, and Linux editions, giving you a lot of choice in your development machine. You'll be needing a Mac if you intend to do iOS development.


Developer Tools: Titanium Studio
The Titanium Studio IDE is based on Eclipse. A single solution can contain projects for multiple mobile targets, which according to Appcelerator leads to 60-90% code reuse.

Depending on type of computer and whether you have previously installed the SDKs for your target mobile platforms, installing and configuring Titanium may be a breeze or it may have you pulling out your hair--it can take a bit of work before you've got everything in place and the right paths and environment variables set up. The Mac setup appears to be the most streamlined in my experience. There's a Getting Started dashboard in Titanium Studio that works to help with all this.

Titanium Studio - Dashboard

Titanium Studio - on a Mac

Debugging Experience
As you would expect, at runtime you can run in a mobile platform emulator, or an actual device connected to your computer.

For iOS devices, getting your app deployed to an iPhone or iPad requires registering your app with the Apple developer center (a standard requirement) and then deploying through iTunes or XCode and syncing with your device--these are some extra steps you don't have to deal with in other mobile development environments, which some may find cumbersome.

For Android devices, things are a bit simpler: you can deploy and run right out of Titanium Studio.


Programming: JavaScript
Here's a fragment of Titanium JavaScript code (from Seven Days with Titanium).

var table1 =  Titanium.UI.createTableView({
style:Titanium.UI.iPhone.TableViewStyle.GROUPED
});

var section1 = Titanium.UI.createTableViewSection();
section1.headerTitle = "Hello";
var row1 = Titanium.UI.createTableViewRow({title:"Hello 1"});
var row2 = Titanium.UI.createTableViewRow({title:"Hello 2"});
section1.add(row1);
section1.add(row2);

var section2 = Titanium.UI.createTableViewSection();
section2.headerTitle = "Hello2";
var row3 = Titanium.UI.createTableViewRow({title:"Hello 3"});
var row4 = Titanium.UI.createTableViewRow({title:"Hello 4"});
section2.add(row3);
section2.add(row4);

table1.setData([section1,section2]);


Here's how this code renders on iOS and Android:

 

Here's another code example from the same blog series that shows playing video:

var videoURL = 'http://movies.apple.com/media/us/ipad/2010/tours/apple-ipad-video-us-20100127_r848-9cie.mov';

var activeMovie = Titanium.Media.createVideoPlayer({
url: videoURL,
width:300,
height:200,
top:50,
left:50,
backgroundColor:'#0f0'
});

view.add(activeMovie);
activeMovie.play();



Capabilities
Titanium allows you good access to mobile platform APIs. Your code can interact with device sensors like the camera, GPS, and accelerometer; and get to resources like Photos, Contacts, and Maps.

Titanium KitchenSink Sample showing Map Access on iOS
 

Online Resources
The Appcelerator Developer Center includes downloads. documentation, training, and community forums.

Key resources:
Quick Start Guide
Training Resources
Sample Code


Appcelerator Cloud Services
Appcelerator offers Appcelerator Cloud Services, which provide Mobile Backend as a Service (MBaaS) functionality. Services provided include push notification, status updates, photo storage, and social integration.



Tutorials
Appcelerator provides both free training resources as well as paid training classes. Titanium also has a certified developer program.


Creating Your First Titanium Apps

This initial tutorial gets you oriented with Titanium and shows how to import sample projects; and also how to create a custom project.

 
 KitchenSink Sample running on Android and iOS

 
Tutorial App running on Android and iOS


Building Native Mobile App with Titanium

This series of tutorials follows the Titanium Certified App Developer (TCAD) course, an introductory training course. They are fairly sparse, not providing a lot of detail.

1a. Native Android Development
1b. Native iPhone Development

These labs guide you through creating, building, and running a simple "Hello, World" app using native Android and iOS tools.

2. Cross-Platform Mobile Development in Titanium

This lab has you begin creation of a cross-platform mobile app called TiBountyHunter, which enables bounty hunters to track a list of fugitives on their mobile device.

Lab 2 - TiBountyHunter

3. UI Fundamentals

This lab has you create the user interface for TiBountyHunter, including table views and windows. populated with dummy data.

Lab 3 - TiBountyHunter with UI and Dummy Data

4. Working with Local Data

This lab uses data from a local SQL database to populate the app's fugitive list.

Lab 4 - TiBountyHunter with data from local SQL Database

5. Working with Remote Data

This lab enhances the TiBountyHunter app to retrieve a list of fugitives from a remote web service.

Lab 5 - TiBountyHunter - with data retrieved from a Web Service

6. Working with Media and the File System

In this lab, you use the camera to add an image to your fugitive and save the image to the file system.

Lab 6 - Camera Support
 

7. Using Location Services

In this lab, you add geolocation support to TiBountyHunter, storing the coordinates of where a fugitive is captured and providing an option to plot location on a map.

Lab 7 - Location Services

8. Appcelerator Cloud Services

In this lab, you integrate Appcelerator Cloud Services so users can boast about their captured bounties.

Lab 8 - Integrated with Cloud Services

9. Integrating Web Content

In this lab, you integrate web content into a partial RSS reader.

Lab 9 - RSS reader

10. Deploy and Distribute

In this lab, you deploy an app for testing on a physical device and follow steps for publishing to the Android Market and iTunes App Store.


Contrasting Xamarin and Titanium
It's hard not to draw parallels between Titanium and Xamarin because they have many similarities. Like Xamarin, Titanium is all about using a familiar language--JavaScript in this case--to do your mobile development. Like Xamarin, Titanium generates native applications and gives your code access to the APIs of your target mobile platforms. Both host an execution layer in their native apps (the Google V8 JavaScript engine for Titanium, Mono/.NET for Xamarin). Titanium has been around a few years longer than Xamarin.

Although they fill the same need and have similarities, it's important to evaluate them individually. If you've decided on a hybrid approach and are torn between the two, take the time to get familiar with each of them and you'll likely find reasons to favor one over the other.


Summary: Titanium Hybrid Mobile Development in JavaScript
Titanium puts mobile development in easy reach of JavaScript developers, the world's largest programmer community. While it's still necessary to learn the APIs and conventions of each mobile platform, taking Objective C and Java out of the equation is nevertheless a major boost. And, being able to use a common language for your iOS and Android projects makes a good deal of code re-use possible.

Next: Getting Started with Mobility, Part 8: Hybrid Development in HTML5 with Icenium



Friday, August 23, 2013

Getting Started with Mobility, Part 6: Hybrid Development in C# with Xamarin

In this series of posts, we're looking at how to get started as a mobile developer. 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. In Parts 2-5, we examined native app development for iOS, Android, Windows Phone, and Windows 8. Here in Part 6, we'll begin examining hybrid development approaches--starting with Xamarin.

Hybrid Frameworks: Assess the Risks
Hybrid mobile frameworks are a mixed bag: they may make all the difference in getting developers up to speed and productive; but they can also bring consequences. Before adopting any hybrid framework, you should get the answers to some key questions:
  • Does the hybrid framework have a runtime layer or execute in a browser, or does it compile to native code?
  • Does it support all of the mobile platforms you need to target?
  • Does it restrict access to some of the features you need?
  • Does it increase overhead or compromise performance?
  • What is its cost? Is it a one-time cost, or a subscription?
  • What are the limitations imposed by the license terms?
  • What risks come with having a third-party dependency?
  • Is it well-established and trustworthy, or is it new and unproven?
  • Does the producer have a good track record of good relations with mobile platform vendors, and are they able to stay in sync with mobile platform updates in a timely manner?
  • Is good support available, including community forums?

Above all, don't make the mistake of thinking all hybrid solutions are similar. They vary greatly in their approach, mechanics, performance, and reliability.

Xamarin: Mobile Development in C#
Xamarin is a third-party product that allows you to develop for iOS and Android in C# and .NET--very familiar territory to Microsoft developers. How is this magic accomplished? From the Xamarin web site: "Write your app in C# and call any native platform APIs directly from C#. The Xamarin compiler bundles the .NET runtime and outputs a native ARM executable, packaged as an iOS or Android app.". In short, Microsoft developers can develop native apps for iOS and Android while working in a familiar language, C#.


Xamarin is the most expensive of the various mobile development approaches we're considering in this series: the Business Edition developer license costs $1,000 per target platform, per developer, per year (there are also Starter, Indie and Enterprise editions available). It may well be worth the cost, depending on what programming language your developers are familiar with.

The Computer You'll Develop on: A PC or a Mac
Xamarin development for iOS requires a Mac; Android development can be done from a PC or a Mac.

Developer Tools: Xamarin Studio
Xamarin provides an IDE, Xamarin Studio, in both Mac OS X and Windows editions. It's also possible to use Xamarin with Visual Studio.

Xamarin Studio requires and makes use of the Software Development Kits for the mobile platform(s) you are targeting--automatically downloading them during installation if it needs to.

For Android development, you can work self-contained in Xamarin Studio: there's a screen editor, allowing you to see what your layout XML looks like visually. For iOS development, on the other hand, you have to use both Xamarin Studio and Apple's XCode side-by-side, so that you can use Interface Builder.

Xamarin Studio
 
Programming: C#
In Xamarin you program in C#, the favorite language of Microsoft.NET developers. Here's an example of Xamarin C# code for iOS (compare to Objective C):
 
var context = CIContext.FromOptions (new CIContextOptions () {
    UseSoftwareRenderer = true
});
var ciImage = new CIImage (cgImage);
var hueAdjustFilter = new CIHueAdjust {
    InputAngle = 3.0f * Math.PI,
    Image = ciImage,
};

var colorControlsFilter = new CIColorControls {
    InputSaturation = 1.3f,
    InputBrightness = 0.3f,
    Image = hueAdjustFilter.OutputImage
};

ciImage = colorControlsFilter.OutputImage;
context.CreateImage (ciImage, ciImage.Extent);

And, here's an example of Xamarin C# code for Android (normally programmed in Java):

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Android1
{
    [Activity (Label = "Android1", MainLauncher = true)]
    public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);
           
            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };
        }
    }
}


Debugging Experience
The debugging experience with Xamarin is similar to that of native iOS and Android development: you can run in the mobile platform's emulator, or on an actual device connected to your computer by USB cable. You're using the same emulators you would use in native development, so they are just as good (or bad, or slow).

In getting Xamarin on my PC to work on actual Android devices, I did need to install a Universal Android Debug Bridge driver.

Android Emulator Selection Dialog
 
Android Emulator

Capabilities
Xamarin doesn't get in the way of using device features. For example, your code can access the Accelerometer and the GPS and the Camera; and can access Contacts and Photos.


AccelerometerPlay Sample
 
CameraAppDemo Sample

Online Resources
The Xamarin web site has a good developer center, whose resources include documentation, guides, recipes, samples, videos, and forums.

Xamarin Developer Center

Key online resources:
Getting Started
Introduction to Mobile Development
Building Cross-Platform Applications
Hello, Android
Hello, iPhone
Android APIs
iOS APIs
Recipes
Sample Code
Videos
Forums

Components Store
Xamarin has a components store; the components offer a broad range of functionality, from charting to syncing with DropBox. Some of the components are free.

Components Gallery

Tutorials
There are a number of good tutorials to be found in the Getting Started area of the Xamarin Developer Center.

1a. Android: Installing Xamarin.Android
1b. iOS: Installing Xamarin.iOS

This tutorial walks you through installation of Xamarin for Android or iOS, and has both Windows PC and Mac editions.

Installing Xamarin on a Mac 

2a. Android: Hello, Android
2b iOS: Hello, iPhone

This tutorial explains how to build a Hello, World application for Android or iOS. It takes you through creating a new project, defining a simple interface and writing simple action code, building the solution, and running it on an emulator.

Creating a New Project in Xamarin Studio

Defining iPhone User Interface in XCode Interface Builder

Hello, iPhone running in iOS Simulator
 
3a. Android: Multi-Screen Applications
3b. iOS: Multi-Screen Applications

This tutorial shows how to create a multi-screen app, with data from the first view passed to the second when a button is pressed.

Android Multi-Screen App Tutorial Running in Emulator



iOS Multi-Screen App Tutorial Running in Emulator

Summary: Xamarin Hybrid Mobile Development in C#
Xamarin puts mobile development in easy reach of Microsoft developers. While it's still necessary to learn the APIs and conventions of each mobile platform, taking Objective C and Java out of the equation is nevertheless a major boost. And, being able to use a common language for your iOS and Android projects makes a good deal of code re-use possible.

Next: Getting Started with Mobility, Part 7: Hybrid Development in JavaScript with Titanium

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