os-eclipse-iphone-cdt-pdf.pdf

(1088 KB) Pobierz
Write native iPhone applications using Eclipse
CDT
How Windows and Linux developers can bypass the iPhone
SDK and write iPhone apps using open source tools
Skill Level: Intermediate
PJ Cabrera
(pjcabrera@pobox.com)
Freelance Software Developer and Writer
Freelance
30 Sep 2008
Learn how to use the Eclipse C Development Toolkit (CDT) to program native
applications for the Apple iPhone, using open source tools to enable iPhone OS
development on any Eclipse-supported platform.
Section 1. Before you start
About this tutorial
In July 2008, with the introduction of the Apple iPhone 3G and the iPhone OS V2.0,
Apple Computer released production versions of an application development
environment for the creation of native iPhone and Apple iPod Touch applications by
third parties. But the application development environment is for Apple Mac OS X
only, leaving developers using the Microsoft® Windows® and Linux® operating
systems out of the loop with regard to developing native applications for this exciting
platform.
This tutorial explores the Eclipse C Development Tooling (CDT) project and how it
allows you to work with C- and C++-based projects. In this tutorial, you install open
Write native iPhone applications using Eclipse CDT
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 1 of 34
developerWorks®
ibm.com/developerWorks
source tools to help develop native applications for the iPhone platform on Windows
and Linux, and learn about the source code of a basic iPhone application in
Objective-C.
Objectives
This tutorial shows how you can use Eclipse and the CDT plug-ins to develop for the
iPhone platform, using the open source GNU Compiler Collection (GCC). You use
these tools to compile an Objective-C example application for the iPhone.
Prerequisites
This tutorial is written for C and C++ programmers whose skills and experience are
at a beginning to intermediate level. You should have a general familiarity using a
UNIX® command-line shell and a working knowledge of the C language. The iPhone
Cocoa Touch native application development frameworks are written in Objective-C
and are based on the Cocoa frameworks used in Mac OS X. Familiarity with
Objective-C and Mac OS X Cocoa application development frameworks is not
assumed, but such skills are helpful in making sense of the iPhone platform. Further
study of Cocoa development is recommended after following this introductory
tutorial.
System requirements
To follow along with this tutorial, you need:
• A computer running Windows or Linux with at least 1.5 GB of free disk
space.
• An installation of Eclipse.
• Permission to write to the /usr/local directory in the file system
(C:/cygwin/usr/local for Windows users).
• Permission to modify your Eclipse configuration by installing plug-ins.
Section 2. The iPhone and iPod Touch platform
The iPhone platform and its cousin, the iPod Touch, are a WiFi-enabled mobile
Write native iPhone applications using Eclipse CDT
Page 2 of 34
© Copyright IBM Corporation 1994, 2008. All rights reserved.
ibm.com/developerWorks
developerWorks®
phone and a mobile Internet device, respectively, with a state-of-the-art graphical
user interface (GUI), a powerful integrated Web browser, and various other
applications, such as e-mail, calendaring, contacts, note-taking, basic Geographic
Information System (GIS) functions through its integration of Google Maps, as well
as entertainment functions, such as movie and music playing.
The iPhone platform is based on the same Darwin operating system and
Objective-C Cocoa frameworks as Mac OS X, modified to fit on an Advanced RISC
Machines (ARM)-based embedded device with 128 MB of RAM, several gigabytes
of flash storage, and a multi-touch touchscreen. The platform also features an
accelerometer for detecting device orientation and movement, and 3-D graphics that
rival the capabilities of the Sony PSP and PS2 consoles.
Darwin, Objective-C, and the Cocoa Touch frameworks
Objective-C is a superset of the C language that adds object-oriented features to C,
but with a syntax much different than that of C++. Objective-C was used by NeXT
Computer in the late-1980s and early 1990s as the development language of choice
for its NextStep and OpenStep operating systems. In the mid-1990s, Apple acquired
NeXT Computer's technology and assimilated much of NextStep and OpenStep into
the Mac OS operating system, producing Darwin and Mac OS X out of the mix.
Darwin is an open source UNIX-based operating system created by Apple as the
core of the Mac OS X platform.
As with NextStep and OpenStep, the development tools for Mac OS X and the
iPhone are based on Objective-C. Apple created a development framework based
on Objective-C for Mac OS X:
Cocoa.
The iPhone platform is based on the same
Darwin operating system core and Objective-C Cocoa framework as the Mac OS X
platform.Cocoa
Touch
is the development framework for the iPhone.
C++ and Objective-C are object-oriented extensions of the C language. But there
are key differences between C++ and Objective-C. Objective-C is a language and an
object-lifetime management runtime, whereas C++ is only a language, and the
programmer manages object lifetime entirely. The other main difference is the
syntax of object-oriented operations between the two. C++ extends the C
struct
manipulation syntax, whereas Objective-C invents a new syntax with scoped square
brackets. Table 1 presents the main syntactic differences between the languages.
Table 1. Main syntactic differences between C++ and Objective-C
Object-oriented
concept
Method invocation
C++
Objective-C
Explanation
C++ extends the C
language syntax for
accessing elements of
a
struct
and reuses it
for method invocations
object_or_class.method(2,
[object_or_class
4);
methodWithParam1:
2 param2: 4];
Write native iPhone applications using Eclipse CDT
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 3 of 34
developerWorks®
ibm.com/developerWorks
on classes or objects.
Objective-C invented a
different syntax with
scoped square brackets
and named parameters
terminated by colons.
Object instantiation
UIView
contentView = new
UIView(x, y,
width, height);
UIView
contentView =
[[UIView alloc]
initWithFrame:
CGRectMake(x, y,
width, height)];
In C++ the
new
keyword allocates
memory for the object
and calls one of the
declared constructors
that most closely
matches the
parameters you pass in
the code. In
Objective-C, you call
the
alloc
method of
the class, which returns
an uninstantiated object
of that class. Then, you
call an
init
method on
the object returned by
alloc
to set initial
values for the instance.
Class definitions
C++ extends the
C
class MyClass
@interface MyClass : NSObject { definition
struct
{
int myVariable;
syntax by adding the
private:
}
ability to define
int myVariable;
methods within a
void privateMethod(); @private
struct
or
class.
It
public:
- (void)privateMethod;
also adds the
private
void setMyVariable(int param);
@public
and
public
keywords
}
- (void)setMyVariable: (int)param; as the
(as well
@end
protected
keyword
not used in this
example) to provide the
object-oriented feature
of encapsulation. In
Objective-C, the
@interface
and
@end
keywords are used to
define the attributes of
a class. Objective-C
uses the keywords
@private, @public,
and
@protected
with
equivalent meaning to
the similarly named
keywords in C++.
Classes in Objective-C
have to inherit from
another class. In this
example, the class
NSObject, whereas in
Write native iPhone applications using Eclipse CDT
Page 4 of 34
© Copyright IBM Corporation 1994, 2008. All rights reserved.
ibm.com/developerWorks
developerWorks®
C++, a class can be the
head of its own class
hierarchy.
Class and instance
methods
C++ uses the
static
class MyClass
@interface MyClass : NSObject { within the
keyword
{
int instanceVariable; class body to mean the
private:
}
method or variable is a
static int classVariable;
class method.
int instanceVariable; + (void)classMethod;
Objective-C uses a
public:
- (void)instanceMethod: (int)param;
+
in front of
prepended
static void classMethod();
@end
a class method and a
void instanceMethod(int param);
prepended
-
in front of
}
an instance method.
C++ adds the scoping
MyClass::privateMethod()@implementation MyClass
{
operator
::
to declare
myVariable++;
- (void)privateMethod { that a method belongs
}
myVariable++;
to a specific class.
}
Objective-C uses the
MyClass::setMyVariable(int param) {
@implementation
myVariable = param; - (void)setMyVariable: (int)param { keywords to
and
@end
}
myVariable = param; mark a group of
}
methods as belonging
@end
to a specific class.
Method declaration
Section 3. Removing application restrictions from your
iPhone
Unravel the background behind iPhone application development and discover how
to create your own iPhone applications.
Jailbreak
When Apple announced the iPhone, the company downplayed the lack of an
unfettered application development environment, citing the need to protect the
cell-provider data network from malicious applications running on hacked phones.
When introducing the iPhone software development kit (SDK) in March 2008, one of
the key features was that the iPhone would only be allowed to run applications
signed by Apple using unique keys handed out to iPhone developers with whom
Apple has a relationship.
Since September 2007, a group calling itself the iPhone Dev Team developed
methods for removing the restrictions Apple devised to limit what could be done on
Write native iPhone applications using Eclipse CDT
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 5 of 34
Zgłoś jeśli naruszono regulamin