Monday, December 5, 2011

FMX iOS/MacOSX SDK encapsulation

One of the best coded libraries, as far as I know, is the OOP VCL. It contains all the possible variations of encapsulating  the Win 32/64 OS SDK. In almost any possible way you look there is an OnEventNotify, or a built class in order to enable the programmer - to enhance his code, or interfere and put his code according to what he would like to program.

However when looking at the FireMonkey, on this stage at least, it lacks these procedures and events. The main focus of the FMX is on the 2d/3d Library abilities. However when one would like to use the FMX as an OS encapsulated tool -- such as within the VCL example -- it will fail.

Nonetheless I am quite sure that Embarcadero programmer and community is on its way to programming the OS related  (MacOSX, Apple mobile iOS -- and furthermore -- the Google Android, or the blackberry) classes encapsulation.

Notification in iOS using Delphi XE2 FMX
An example where I encountered this lack of OS (in my case iOS) SDK encapsulation -- is on the following example:

I've been programming a small application for iOS using the Embarcadero Delphi XE2 FMX. One portion of the code was dedicated to sending an iOS notification messages -- which is a service that Apple interduced for programmers in order to send Messages much like the SMS architecture -- only the notifications are the wifi messages. Registrating the  Push notifications on iOS, and registrating the deviceID in the remote server was a part of the code that needed to be done.

The following code is a xcode iOS objective-c code, demonstration  of the code to put -- on the sample the code is apart of the ApplicationDelegate.m file on the iOS, the ApplicationDelegateProtocol is well documented under the pages:

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | 

    return YES;

- (void)application:(UIApplication*)application 
    NSLog(@"My token is: %@", deviceToken);
As you can see the code uses two objective-c events, that make the device notification part possible.
However I had to translate that from objective-c to FPC objective-pascal.The important thing is where is code will be put in. On the FMX platform the Application Delegation is being done as an object that register the main Form of the XE2 FMX application. 
The following code was taken from the FMX_Platform_iOS.pas file from the source of the FMX code:

  ApplicationDelegate = objcclass(NSObject)
   procedure applicationDidFinishLaunching(notification: UIApplication); 
message 'applicationDidFinishLaunching:';
   procedure applicationWillTerminate(notification : UIApplication); message 
Extending the ApplicationDelegate objclass can be done by a 'helper' class of the objective-pascal called a category -- which according to the documentation will virtually extend the objclass. However, there is an obstacle the ApplicationDelegate objclass is being implemented in the unit, but isn't being introduced to other units, meaning it isn't being registered in the interface part of the unit.

No comments:

Post a Comment