iPhone/iPod development

From Lazarus wiki
Jump to navigationJump to search

The first version of FPC for the iPhone SDK 2.x is available.

Installation

Requirements:

  • Intel Mac (because the iPhone SDK requires an Intel Mac)
  • iPhone SDK 2.x installed (only tested with 2.2 though) (note: SDK requires Leopard 10.5.5 or higher)
  • FPC 2.2.2 or a later FPC 2.2.x release installed

Some features:

  • Simulator support
  • the Xcode template transparently supports (requires, actually) integrating Pascal and Objective-C source files into a single application
  • Pascal translation of the OpenGL ES 1.1 C-header from the SDK (this one we can distribute, because it's licensed under an SGI open source license), available in the gles11 unit

Please read the entire ReadMe.rtf on the installer disk image before asking for help. It contains quite a bit of information on things to watch out for (especially when using the Simulator), and how to change the included template when adding extra Pascal source files.

Note that unlike other FPC releases, this installer will install the FPC sources (of FPC 2.3.1, svn revision 12531) and a script, and will then launch this script in order to compile FPC (after you tell it where you installed the iPhone SDK, so it can generate a Pascal version of some required C headers). This modus operandi is necessary to comply with the iPhone SDK agreement's stipulation that no derived works of the SDK may be distributed. If the script aborts for some reason, you can find it at /Developer/FreePascalCompiler/iPhoneSnapshot-2.3.1-r12531/InstallScript/finish_fpc_iphone_install.command to relaunch it.

The installer disk image is available here (21MB -- there are also advantages to only distributing the source): ftp://ftp.freepascal.org/fpc/snapshot/v23/arm-macosx/fpc-2.3.1v2.arm-iphone.dmg

If you encounter problems with the script that is launched when the installer finishes the installation, please always provide the entire contents of the Terminal window in which the script was executed when asking for help.

Apple's Licence limitations

Developing applications for iPhone/iPod

FPC cannot work around the requirement of needing an Apple-issued certificate to run self-written programs on an iPhone/iPod Touch. So you have to sign up to Apple's iPhone Developer program ($99/year) to run FPC-compiled programs on the real hardware (just like for regular Xcode-compiled programs).

Without such a certificate, you can only run programs on the simulator (possibly unless you have a jailbroken iPhone/iPod Touch, but in that case you are on your own).

Read more about Apple's iPhone development program here: http://developer.apple.com/iphone/program/

Pascal headers required for developing

It is against the iPhone SDK agreement to distribute derivative works of this SDK, and this includes Pascal translations of framework headers. So it would be quite a challenge (if possible at all) to create and distribute the sources of a pure Pascal program with a GUI that does not violate the iPhone SDK agreement terms at this time. See the ReadMe.rtf (included in the .dmg above) file for more information

Technical Development Limitations

At this time is not known whether and if so how it is possible to copy an application to an iPhone/iPod Touch and to execute it without using Xcode. It's of course possible to write the application using any IDE available (Lazarus, Lightweight IDE, etc) and it should also be possible to make any of them work with the Simulator. However, any application has to be built by Xcode for it to be uploaded to the Device and to debug it.

iPhone Simulator is not iPhone

It does not make much sense to run test programs in the Simulator, because the Simulator allows full access to the real Mac OS X, so you have no idea whether you are testing Mac OS X/iPhone-specific code or regular Mac OS X/i386 code. It's probably safer (and definitely much easier) to run them under plain Mac OS X/i386 in that case. The Simulator is only really useful if you have a bug that takes a long time to reproduce and which occurs both in the Simulator and on real hardware.

iPhone Simulator:

iPhone Simulator implements the iPhone OS API in a Mac application, providing a runtime environment that mimics the environment devices provide. By allowing you to run your applications in Mac OS X, the simulator provides a way to quickly test your application’s functionality without the need for an actual device. However, running applications on the simulator is not the same as running them on devices.

The simulator uses Mac OS X versions of the low-level iPhone OS frameworks instead of the versions that run on devices. But, in general, the simulator is a great tool for performing initial testing of your application. Keep in mind that, because the simulator does not emulate device functionality, you must always perform final testing and performance analysis of your application on actual devices.

Compiling and running pure Pascal applications on an iPhone/iPod Touch

Plain command line applications work fine on the iPhone/iPod Touch, except that the output will only be visible in the gdb console (if run or debugged from Xcode; press cmd-shift-r, or select Run->Console) or in the device's console log (if started from the iPhone/iPod Touch itself). In case of GUI applications, you will probably require non-distributable header translations due to licensing restrictions as outlined above. You can however still use your own private Pascal header translations (possible generated by an automated tool).

Using only Xcode

  1. Create a new project based on the template
  2. Remove all files except for *Prefix.pch, Info.plist, Frameworks and Products from the project
  3. Add your own Pascal sources (Project->Add to Project...)
  4. Double-click on the project icon at the top left in the Xcode file list, go to the Build tab, and change the FPC_MAIN_FILE setting to the name of the test program you added (for all configurations).
  5. Build the program

Combining command-line compilation and Xcode

If you compile a program on the command line (for Darwin/ARM) and copy it over the built binary in an Xcode project, then if you "run" the program in Xcode, it will code sign and run the binary that you copied over rather than the original program (just make sure that the binary in Xcode has been compiled recently so there are no changes that trigger Xcode to relink it). You can do this using a project based on the default template, without changing anything to the project.

You can find the Xcode binary's location by opening the Products folder in the project overview, right-clicking (ctrl-clicking) on the .app file and selecting "Reveal in Finder". The binary's location is then in program.app/Contents/MacOS (replace program.app with the actual name of the folder selected in the finder). Just overwrite whatever file is in this MacOS folder with the program you compiled (e.g. using the command line cp program).

To compile for Darwin/ARM on the command line (replace "iphone2.1" with the directory in which you installed the SDK):

ppcarm -FD/iphone2.1/Platforms/iPhoneOS.platform/Developer/usr/bin -XR/iphone2.1/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk program.pas

To compile for the simulator, replace "ppcarm" with "/usr/local/lib/fpc/2.3.1/ppc386", and the -XR parameter with the path to a simulator SDK (e.g., -XR/iphone2.1/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.0.sdk). (for advanced users) And in case you created an alternate fpc.cfg that uses a different unit search path than /usr/local/lib/fpc/2.3.1/units/*, also add -Fu/usr/local/lib/fpc/2.3.1/units/*


Compiling and running pure Pascal applications on iPhone simulator

Compile and linking requires a usual ppc386 compiler. There's little difference between iPhone simulator and Mac OS X application. The major difference, is that iPhoneSim applications are linked to frameworks other than usual OS X application. Some frameworks are iPhone specific (UIKit), other frameworks can exists for both OS X and iPhone (CoreFoundation, CoreGraphics etc). iPhoneSim SDK provides it's own versions of these frameworks and you should make sure, that SDK's versions are used. to do so, you'll need to add linker switch while compiling an applicaiton:

 fpc simtest.pas -k-syslibroot -k/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.0.sdk

where /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.0.sdk is a root to your iPhoneSim sdk folder. It might vary depending on there you've installed XCode.

There're a atleast 3 versions of iPhone SDK available 2.0, 2.1, 2.2 depending on the version you need, the path also changes:

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.0.sdk for SDK version 2.0

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.0.sdk for SDK version 2.1

etc...

"Uploading" application to iPhone Simulator

Simulator applications are located at:

 ~/Library/Application Support/iPhone Simulator/User/Applications/

each of application has a directory, and a file .sb

Name of the directory does not have any meaning, the simulator scans for the contents of the directory, looking for the bundle.

Inside each bundle there's an OS X executable (the same style as usuall Max OS X applications have), you can place newly compiled application there by coping or creating a symbolic link to the actual location on the disk.