Difference between revisions of "iPhone Laz Extension"

From Lazarus wiki
Jump to navigationJump to search
Line 49: Line 49:
 
* Open and install the package
 
* Open and install the package
 
* You will find iPhone options group added to both Project and Environment options
 
* You will find iPhone options group added to both Project and Environment options
 +
[[Image:projoptions.png]]
  
 
== How to use ==
 
== How to use ==

Revision as of 23:37, 4 January 2010

About

iPhone Laz extension is Lazarus IDE extension that's designed to simplify iPhone applications development.

Author

Dmitry 'skalogryz' Boyarintsev

License

modified LGPL (same as the FPC RTL and the Lazarus LCL). You can contact the author if the modified LGPL doesn't work with your project licensing.

Download

The latest stable release can be found on todo: link to the lazarus-ccr sf download location.

Change Log

  • Version 0.5 05 Jan 2010

Dependencies / System Requirements

  • Lazarus 0.9.29 (SVN trunk) or higher. (for Project options integration)
  • Mac OS X 10.5 (Intel) + XCode 3.1 or higher
  • Mac OS X 10.6 + XCode or higher (untested)
  • FPC 2.5.1 (or higher) with Objective-P syntax is recommended to use.
  • Since Apple doesn't provide any tools for development on other than OS X system, the package cannot (should not) be installed and used on Windows or Linux.

Status: Stable / Alpha

Known Issues:

  • Project launched from Lazarus fails with an exception. It should be launched from the iPhone simulator
  • The project, using iPhoneAll unit, compiled for the simulator must use Smart Linking switch (-XX)
  • Then application's title is changed (in Project options) a string is added to the main file
 Application.Title:='MyApp';

It must be deleted manually

  • XCode sometimes fails to debug the application on iDevice, though it's loaded successfully. Don't worry. Your application is still on the iDevice and ready to be tested.
  • todo:

To do:

  • XIB files support
  • Resources files support
  • Improve debugging abilities

Installation

In order to develop for iPhone or iPod, you'll need arm FPC compiler with rtl and packages installed in the system. There're 2 ways to do it: built manually via SVN or installed from a special FPC arm package. Both ways are described here.

You don't need to install ARM compiler or packages, if you're planning to use iPhone simulator only.

  • Get the package source via svn or download from Sourceforge
  • Open and install the package
  • You will find iPhone options group added to both Project and Environment options

projoptions.png

How to use

Common usage

  • Mark a project as iPhone project (using Project menu -> is iPhone project or Project Options -> iPhone -> is Phone project application)
  • To change the displayed name of the application you must change the application Title in Project Options.
  • You can also change the application icon in the same way (not implemented, yet)

Simulator

  • To install the application to iPhone simulator you need to build the project (ctrl+F9). Don't run the project or it will fail.
  • Run the iPhone Simulator, your application should be on the one of the iPhone simulated pages.

Iphonesim.png

iPhone/iPod using XCode

There's not way to install an application to iPhone, but using XCode. (Jailbreaks have their own way, not used by the package). The package provides an utility to make an XCode project from Lazarus project.

  • Write the program (and test it on the Simulator, if necessary)
  • Select Project -> Update XCode project. If not exists xcode directory is selected there you'll find xcode project.
  • Open the project. Select iPhone device for as Active SDK.
  • Press Build and Go. This should build the application and install it to iPhone device.

xcodeproj.png

Note:

  • You'll need arm-darwin FPC compiler installed and configured. Any units or package used, must be compiled for arm-darwin target as well.
  • Created XCode project, is not flexible enough to build the project for iPhone Simulator, so make sure that iPhone OS is used as active SDK.

Hello World

The sample requires a objective-p compiler (fpc 2.5.1 or higher) with cocoaint packages compiled for iPhone <delphi>

program project1;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$linkframework UIKit} 

uses
  iPhoneAll, CGContext, CGGeometry, CFString; 

type
  TAppDelegate = objcclass(NSObject)
    procedure applicationDidFinishLaunching(application: UIApplication); message 'applicationDidFinishLaunching:';
  end; 

  TMyWindow = objcclass(UIWindow)
  public
    procedure drawRect(c: CGRect); override;
  end;

const
  helloworld = 'Hello world'; 

// window paint method
procedure TMyWindow.drawRect(c: CGRect);
var
  cg : CGContextRef;
begin
  // getting current context
  cg:=UIGraphicsGetCurrentContext;
  // setting back ground color
  CGContextSetRGBFillColor(cg, 0, 0, 0.5, 1);
  CGContextFillRect(cg, c); 

  // rotating up-side down context
  CGContextTranslateCTM(cg, 0, c.size.height);
  CGContextScaleCTM(cg, 1, -1); 

  // setting text color
  CGContextSetRGBFillColor(cg, 1, 1, 0, 1);
  CGContextSetRGBStrokeColor(cg, 1, 1, 0, 1);
  // setting font  (must set any)
  CGContextSelectFont(cg, 'Helvetica', 30, kCGEncodingMacRoman);
  // rendering text
  CGContextShowTextAtPoint(cg, 0, c.size.height-50, helloworld, length(helloworld));
end;

var
  mainwindow : TMyWindow;

{ TAppDelegate }

procedure TAppDelegate.applicationDidFinishLaunching(application: UIApplication);
begin
  // application has initialized, now we can create the main window
  mainwindow:=TMyWindow(TMyWindow.alloc);
  // initialize window in Objective-C style
  mainwindow := TMyWindow(mainwindow.initWithFrame (UIScreen.mainScreen.bounds));
  // activate and show the window
  mainwindow.makeKeyAndVisible;
end;

function NSStr(const s: string): NSString;
begin
  // converting string to NSString (CFStringRef and NSString are interchangable)
  Result:=NSString( CFStr(PChar(s)));
end;

var
  pool    : NSAutoreleasePool;
begin
  // initialize foundation memory manger (aka autorelease pool)
  pool := NSAutoreleasePool.alloc.init;
  // launching main application loop
  ExitCode:=UIApplicationMain(argc, argv, nil, NSSTR('TAppDelegate'));
  // according to docs the UIApplicationMain never returns,
  // but still the code present in the Obj-C main.m files
  pool.release;
end.

</delphi>

The result would look like:

iphonescreenshot.png