Can Delphi XE4 import iOS ObjC Class ? ( Static Li

2019-05-05 16:54发布

问题:

Can Delphi XE4 import iOS ObjC Class ? ( Static Library *.a )

ObjC Code : test.a

// test.h ---------------------------------------------------------------
#import <Foundation/Foundation.h>
@interface mycalc : NSObject {
   BOOL busy;
  }
- (int) calc : (int) value;
@end


//  test.m --------------------------------------------------------------
#import "test.h"
@implementation mycalc
 -(int) calc:(int)value {
    busy = TRUE;
    return ( value + 1);
  }
@end

Delphi XE4 Code "Unit1.pas" for Testing

Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, 
  System.Variants,System.TypInfo,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  //
  Posix.Dlfcn,
  Macapi.ObjectiveC,Macapi.ObjCRuntime,
  iOSapi.CocoaTypes,iOSapi.foundation,iOSapi.uikit,iOSapi.CoreGraphics;

{$Link libtest.a} // <-- ObjC Static Library (3 Architectures included)
                  //     But Compiled binary size is always same,
                  //     with or without "$Link libtest.a" lines.

type
  //
  mycalc = interface(NSObject)
   ['{12891142-0F45-410D-A9EF-212F1AE23294}'] // Any Unique GUID
   function  test(value : integer) : integer; cdecl; 
  end;

  mycalcclass = interface(NSObjectClass)
   ['{42891142-0F45-410D-A9EF-212F1AE23295}'] // Any Unique GUID
   end;

  //the TOCGenericImport maps objC Classes to Delphi Interfaces 
  // when you call Create of TObjc_TestClass
  TmycalcClass = class(TOCGenericImport<mycalcclass, mycalc>) end;

  // -------------------------------------------------------------------------
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
 Var
  occalc : mycalc;
  rtn    : integer;
  handle : integer;
begin
 handle := dlOpen('libtest.a',RTLD_LAZY); // ## Q2 : Is It Right ?
 occalc := TmycalcClass.Create;           // ## Q3 : Error :
                                          //          Can't found ObjC Class mycalc  
 rtn := occalc.test(100);                  
 Button1.Text := 'rst:' + IntToStr(rtn);
end;

end.

I rewrite the code with Lars' advice XE4 ( Firemonkey + iOS Static Library) , Pascal conversion from Objective C Class?, Compiling is OK.

But, After running the program in iPad3 device, When press the button, It shows "ObjectiveC class myclac cound not be found"

My Questions are

Q1. Is it right syntax for XE4 & iOS Static Library ?

Q2. with / wihtout Line {$Link libtest.a} Compiled size is same always. What's is wrong in my code ?

Q3. I know iOS App can't use static library except apple library. So, dlOpen is not required. Is it right ?

Always Thanks

Simon, Choi