Book Image

Expert Delphi

By : Paweł Głowacki
Book Image

Expert Delphi

By: Paweł Głowacki

Overview of this book

Delphi is the most powerful Object Pascal IDE and component library for cross-platform native app development. It enables building natively compiled, blazingly fast apps for all major platforms including Android, iOS, Windows, Mac, and Linux. If you want to build server-side applications, create web services, and have clear GUIs for your project, then this book is for you. The book begins with a basic primer on Delphi helping you get accustomed to the IDE and the Object Pascal language and will then quickly move on to advanced-level concepts. Through this book, we’ll help you understand the architecture of applications and will teach you the important concepts of the FireMonkey library, show you how to build server-side services, and enable you to interact with the Internet of Things. Towards the end, you will learn to integrate your app with various web services and deploy them. By the end of the book, you will be able to build powerful, cross-platform, native apps for iOS and Android with a single code base.
Table of Contents (14 chapters)

Deploying to mobile devices

Our DelphiHelloWorld project is now ready for deployment to mobile devices. We have already built and run it on Windows. Now, we are going to first deploy it to an Android device and then to iOS.

This is a one-time preparation for mobile development that you need to go through after Delphi is installed. We want to get to the point that you can see your devices as targets inside the Project Manager.

For Android, all steps to configure your system to detect your device are described at http://docwiki.embarcadero.com/RADStudio/en/Configuring_Your_System_to_Detect_Your_Android_Device.

Deploying to Android

Deploying apps from Delphi to Android devices is simpler then deploying to iOS. You only need to have an Android device and a USB cable to connect your device to a Windows machine where you have Delphi installed.

Before deploying to an Android device, you need to find out if your device is supported by the version of Delphi you are using. For details, consult http://docwiki.embarcadero.com/RADStudio/en/Android_Devices_Supported_for_Application_Development for the list of currently supported Android versions.

The first step is to enable USB debugging on the device. This option can be set in Developer options on your device. This option is not easy to find. It is, in fact, hidden. On your Android device, go to the Settings | About menu. Tap the Build number seven times to make Settings | Developer options available. Then, check the USB Debugging option. Connect your Android device with a USB cable to a computer where your PC is running. If you are using VMware make sure that your Android device is connected to the virtual machine where you have Delphi and not to your Mac. The first time you connect your Android device, you may see a message displayed on the device to Allow USB debugging from the computer, identified by a string of hexadecimal numbers that represents the computer's RSA key fingerprint. Check the Always allow from this computer option and click on OK.

More info on enabling USB debugging on Android devices is available at http://docwiki.embarcadero.com/RADStudio/en/Enabling_USB_Debugging_on_an_Android_Device.

The next step is to install the USB driver for your Android device on the machine where Delphi is installed. The test device that I'm using is Google Nexus 7.

Probably the easiest option to download all the necessary bits for Android development is to download and install Android Studio for free from Google. It will download all necessary Java SDKs and tools that are needed in order to build an Android apk executable file. For reference, I'm using Android Studio version 2.2.2. On the splash, select the Android SDK option. In the dialog, select the SDK Tools tab and check the Google USB Driver option. Click OK and the driver will be downloaded and installed, as shown in the following screenshot:

Android SDK Tools with the "Google USB Driver" option checked

In order to make sure that the device driver has been installed successfully, go to the Device Manager window in Control Panel and you should find your device listed. If you right-click on the device, there is an option to Update Device Driver. I could find my Nexus 7 listed there and the device driver was up to date. Refer to the following screenshot:

Nexus 7 in Device Manager

Depending on your device, the steps may be different. You can find detailed information on how to install the USB driver for your Android device at http://docwiki.embarcadero.com/RADStudio/en/Installing_the_USB_Driver_for_Your_Android_Device.

Now, back to Delphi IDE. Make sure that our DelphiHelloWorld project is still open in the IDE. In the Project Manager, expand Target Platforms and click on Android. The first time you click on the Target node, you will see the message Android SDK tools are required. Do you want to download and install Android SDK tools automatically?. Click on Yes. The process to download and install SDK tools will start and you will see in the Command Prompt window the progress of the operation. After a moment, you should see your device listed under Tools in the Project Manager.

Double-click on the Android node to select the Android target. It should display in bold font. Now this is the current target, so when you click on the Run green arrow, the IDE will build our Delphi Hello World app using the Delphi Android compiler and the executable apk Android file will be created, deployed to the device, and run in just one operation.

Click on Run. The project will be compiled, deployed to your Android device, and run:

Delphi "Hello World" app running on Android

You can find more information about preparing your IDE for running apps on Android devices on DocWiki at http://docwiki.embarcadero.com/RADStudio/en/Android_Mobile_Application_Development.

Deploying to iOS

Deploying apps from Delphi to an iOS device requires a Mac computer. As compared to Android, deploying to iOS requires more steps because you also need to enroll for the Apple iOS Developer Program and to provision the device you want to deploy to.

First, you need to make sure that the iOS device that you want to deploy your application to is compatible. In case of iPhone, the oldest supported model is 4GS, but you want to have a device that can have iOS 10 installed. In my case, it is iPhone 6S.

The first steps to prepare your Mac and an iOS device for deployment are exactly the same that any iOS developer needs to do. This is something that you do once. During the process of deploying to an iOS device, an app needs to be digitally signed with the special command-line tool that comes with Apple Xcode installation. You could install just the Xcode command-line tools, but in practice it is easier to install the whole Xcode development environment from Apple.

In order to be able to deploy apps to an iOS device, it needs to be provisioned first. The process of provisioning iOS devices, joining the Apple iOS Developer Program, and installing various certificates is the same as in the case of any app developed with Xcode. In order to be sure that you will be able to deploy an app from Delphi into iOS, it is recommended to create an empty app from a template in Xcode and try to run it on the device. If this is successful, the next step is to install a special PAServer Mac program on the machine where you have Xcode installed. The PAServer19.0.pkg installable package is installed in the PAServer subdirectory of Delphi. Just copy it over to your Mac and execute. This will install the PAServer. The PAServer is implemented as a command-line app, and after it is launched on Mac, it will display a command-line interface where you will be prompted to enter your Mac password. Your Mac will also display a dialog asking if it is OK to give PAServer debugging permissions. Accept it.

You can think about the PAServer as an agent that performs operations on behalf of the Delphi IDE that runs on a remote Windows machine. In order for Delphi to be able to connect, you need to know the IP address of the machine where the PAServer is running. You can find out the IP address by issuing the i command from the PAServer's Command Prompt. The list of all available command-line parameters to PAServer is displayed when you enter ? in the Command Prompt, as shown in the following screenshot:

PAServer running on Mac

Now we need to configure Delphi, so it can find the iOS device you want to deploy to. The goal is to find your device listed in the Project Manager. In general, you could have multiple different Macs visible to your Delphi installation. For every Mac you want to deploy to, you need to define a connection profile.

Make sure that our DelphiHelloWorld project is open in the IDE.

Double-click on the iOS Device - 64 bit node under Target to make it active. It will be displayed in bold. Now, right-click on the node and select Properties. You will see a small Platform Properties dialog where you will be able to select the proper iOS SDK. Click on the SDK combo box and select Add New... from the dropdown.

Then, select Add New from the connection profile combo. The Create a Connection Profile wizard will be displayed. I have entered DelphiDev as the profile name. Click on Next. On the next tab of the wizard dialog, we need to enter the address and port of the remote machine. Keep the default port 64211 and enter your Mac password in the last field. Now click on the Test Connection button to verify that the IDE can communicate with the remote PAServer. If everything went well you should see the message that the connection to your Mac succeeded. Click on Finish, as shown in the following screenshot:

"Create a Connection Profile" dialog

Now, back in the Add a New SDK dialog, select the SDK version to add and click on OK:

Add a New SDK dialog

This will initiate the process of updating the local file cache, with files being downloaded from Mac that are needed by the Delphi iOS compiler to generate an iOS executable file. Refer to the following screenshot:

Updating Local Cache

After the cache is refreshed, the IDE should automatically update the iOS 64-bit Target node and you should see your device listed there:

Project Manager with an iPhone device listed as a target

Now click on the Run button. This will locally build the iOS ipa executable file and send it to the Mac machine where PAServer is running. The PAServer will invoke the command-line tool from the Xcode installation to digitally sign the executable, and then it will be deployed and run on the physical iOS device--all in one operation.

And here is how our Delphi Hello World app looks on my iPhone 6 running iOS 10:

Delphi "Hello World" app running on iOS

More information about deploying apps to iOS devices from Delphi can be found online at http://docwiki.embarcadero.com/RADStudio/en/IOS_Mobile_Application_Development.