Skip to content

Instantly share code, notes, and snippets.

@cnsoft
Last active December 19, 2015 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnsoft/5881812 to your computer and use it in GitHub Desktop.
Save cnsoft/5881812 to your computer and use it in GitHub Desktop.
Last step of admob integrated with delphi xe4.
setense:
Undefined symbols for architecture armv7s:
"_OBJC_CLASS_$_ASIdentifierManager", referenced from:
Undefined symbols for architecture armv7
http://www.cocoachina.com/bbs/simple/?t117929.html
xiasix 2012-11-20 12:31
If you're using the Admob v6.2 library make sure you're NOT using the -all_load linker flag, use the -ObjC linker flag instead. 这个原因 找到了
xe4 怎么办呢?
I got this error.
"_OBJC_CLASS_$_ASIdentifierManager", referenced from:
#try to add this line to loaded symboal?
function XE4_FakeLoader : ZBarReaderView; cdecl; external 'libzbar.a' name 'OBJC_CLASS_$_ZBarReaderView';
[출처] Delphi XE4 with iOS / #07. ZBar Interface|작성자 최원식옹
//Important Step.
// Adding "iAd" Framework
// Step 1. Select Tools - Options - SDK Manager
// Step 2. Add Framework /System/Library/Frameworks iAd
// Step 3. Update Local File Cache//
//
[출처] Delphi XE4 with iOS / #13. Procedure for adding Apple Framework|작성자 최원식옹
http://blog.naver.com/PostView.nhn?blogId=simonsayz&logNo=120190032530&parentCategoryNo=20&categoryNo=&viewDate=&isShowPopularPosts=false&from=postView
// 2.2. Static Library /WO Source
// Find Any Class Symbol in *.a , and Define func.
// To Find Class Symbol : NM libtest.a [Enter]
// function XE4_FakeLoader : mycalc; cdecl; external 'libtest.a' name 'OBJC_CLASS_$_mycalc';
[출처] Delphi XE4 with iOS / #05. Hybrid Coding with Objective C Static Library |작성자 최원식옹
//
http://sourceforge.net/projects/dpfdelphiios/files/latest/download
//how to add more framework. (step)
@cnsoft
Copy link
Author

cnsoft commented Jun 28, 2013

unit iOsEmail;

(* ----------------------------------------------------------------------------
email component and function for IOS
author: Xavier Dufaure de Citres, 2012
Credits:

usage: 1. add the component (TiOSEmail) to your form and use iOSEmail1.sendEmail;
to trigger the display of the email on the device
2. call the iosSendEmail directly (see below)

note: you can only have one instance of the componnent, if you have a component
create you cannot use the direct call function.

version 0.90 June 1st 2012

TODO
- handle to,CC and BCC ( TiOSEmail.SendEmail )
- detect mime type ( TiOSEmail.SendEmail.addAttachement )
- retreive the to,cc and BCC used (not sure it's possible, but would allow to restore last To used)


*)
{$IFDEF FPC}
{$mode delphi}
{$modeswitch objectivec2}
{$ENDIF}

interface

uses SysUtils, Classes, FMX_Forms,FMX_Types{$IFDEF FPC}, iPhoneAll,FMX_Platform_iOS {$ENDIF};

type TEmailresult = ( erSent, // email was sent succesfully
erCannotSendEmail, // email is not configured on the device
erCancelled, // user canceled the email without saving it
erSaved, // user canceled the email but saved it int he draft
erFailed, // email failed to be sent
erUknown); // unknown error, assume failure

type
MFMailComposeResult = integer;
const
MFMailComposeResultCancelled = 0;
MFMailComposeResultSaved = 1;
MFMailComposeResultSent = 2;
MFMailComposeResultFailed = 3;

{$IFDEF FPC}

{$linkframework MessageUI}

type
MFMailComposeViewController = objcclass external (UINavigationController)
private
_internal: id;
public
class function canSendMail: Boolean; message 'canSendMail';
procedure setMailComposeDelegate (newValue: id); message 'setMailComposeDelegate:';
function mailComposeDelegate: id; message 'mailComposeDelegate';
procedure setSubject(subject: NSString ); message 'setSubject:';
procedure setToRecipients(toRecipients: NSArray); message 'setToRecipients:';
procedure setCcRecipients(ccRecipients: NSArray); message 'setCcRecipients:';
procedure setBccRecipients(bccRecipients: NSArray); message 'setBccRecipients:';
procedure setMessageBody_isHTML(body: NSString; isHTML: Boolean); message 'setMessageBody:isHTML:';
procedure addAttachmentData_mimeType_fileName(attachment: NSData; mimeType: NSString; filename: NSString); message 'addAttachmentData:mimeType:fileName:';
end;

Type
TMailDelegate = objcclass(NSObject)
Emailcontroler : MFMailComposeViewController;
Done : Boolean;
EmailResult : MFMailComposeResult;
procedure mailComposeController_didFinishWithResult_error(
controller: MFMailComposeViewController;
result : MFMailComposeResult;
error: NSErrorPtr); message 'mailComposeController:didFinishWithResult:error:';
end;
{$ENDIF}

type
TiOSEmail = class(TFmxObject)
private
{ Private declarations }
FSubject : String;
FRecipientsTo : TstringList;
FRecipientsCC : TstringList;
FRecipientsBCC : TstringList;
FBody : String;
FisHTML : boolean;
FFileAttachment: TstringList;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Subject: String read FSubject write FSubject;
property RecipientsTo : TstringList read FRecipientsTo write FRecipientsTo;
property RecipientsCC : TstringList read FRecipientsBCC write FRecipientsBCC;
property RecipientsBCC: TstringList read FRecipientsBCC write FRecipientsBCC;
property Body: String read FBody write FBody;
property isHTML: boolean read FisHTML write FisHTML default True;
property FileAttachment: TstringList read FFileAttachment write FFileAttachment;
Function SendEmail : TEmailresult;
end;

procedure Register;

function iosSendEmail( recTo,recCC,recBCC,subject,body : string; //comma delimited
FileAttachment : TstringList = nil;
isHTML: boolean = true ): TEmailresult;
{
examples
iosSendEmail('Xavier@domain.com','','','my subject','

Hi

how are you?')
iosSendEmail('Xavier@domain.com, michelle@domain.com','','','my subject','

Hi

how are you?')
}

implementation

{$IFDEF FPC}
procedure TMailDelegate.mailComposeController_didFinishWithResult_error(
controller: MFMailComposeViewController;
result : MFMailComposeResult;
error: NSErrorPtr);
Begin
//remove controler from screen
controller.dismissModalViewControlleranimated(true);
// to break the waiting loop
done := true;
EmailResult := result;
end;

const MailDelegateVar : TMailDelegate=nil;
{$ENDIF}

constructor TiOSEmail.Create(AOwner: TComponent);
begin
Inherited Create(Aowner);
{$IFDEF FPC}
if Assigned(MailDelegateVar) then
raise Exception.Create('can only have one of these component');

//allocate the delegate
MailDelegateVar := TMailDelegate.alloc.init;
{$ENDIF}
//init property
FSubject := '';
FRecipientsTo := TstringList.create;
FRecipientsCC := TstringList.create;
FRecipientsBCC := TstringList.create;
FBody := '';
FisHTML := true;
FFileAttachment:= TstringList.create;
end;

destructor TiOSEmail.Destroy;
begin
{$IFDEF FPC}
MailDelegateVar.release;
MailDelegateVar := nil;
{$ENDIF}
//free objects
FRecipientsTo.free;
FRecipientsCC.free;
FRecipientsBCC.free;
FFileAttachment.free;
inherited;
end;

function TiOSEmail.SendEmail: TEmailresult;
{$IFDEF FPC}
var NS : NSstring;
//anArray: NSMutableArray;
tempresult : integer;
I : integer;

procedure addAttachement(filename:string);
var attachment: NSData;
mimeType: NSString;
Begin
//addAttachmentData_mimeType_fileName(attachment: NSData; mimeType: NSString; filename: NSString)
if FileExists(filename) then
Begin
attachment := NSData.alloc.initWithContentsOfFile( NSSTR(PChar(filename) ) );
mimeType := NSSTR(PChar('text/plain') );
filename := extractfilename(filename); //no need of the path anymore
MailDelegateVar.Emailcontroler.addAttachmentData_mimeType_fileName( attachment, mimeType,NSSTR(PChar(filename)) );
attachment.release;
end;
end;

{$ENDIf}
Begin
{$IFDEF FPC}
//set the email control and assign its delegate
MailDelegateVar.Emailcontroler := MFMailComposeViewController.alloc.init;
MailDelegateVar.Emailcontroler.setMailComposeDelegate(MailDelegateVar);

if MailDelegateVar.Emailcontroler.canSendMail=false then
Begin
result := erCannotSendEmail;
MailDelegateVar.Emailcontroler.Release;
exit;
end;
//ok we can send email

//set subject
NS := NSSTR(PChar(FSubject));
MailDelegateVar.Emailcontroler.setSubject(NS);

//set to
(*anArray := NSMutableArray.Alloc.init;
for i := 0 to FRecipientsTo.count -1 do
anArray.addobject( NSString(PChar(FRecipientsTo[i]) ) );
MailDelegateVar.Emailcontroler.setToRecipients(anArray);
anArray.release;

//set cc
anArray := NSMutableArray.Alloc.init;
for i := 0 to FRecipientsCC.count -1 do
anArray.addobject( NSSTR('test@totot.fr') );
MailDelegateVar.Emailcontroler.setCCRecipients(anArray);
anArray.release;

//set bcc
anArray := NSMutableArray.Alloc.init;
for i := 0 to FRecipientsBCC.count -1 do
anArray.addobject( NSSTR(PChar(FRecipientsBCC[i]) ) );
MailDelegateVar.Emailcontroler.setBCCRecipients(anArray);
anArray.release;
*)
//body
MailDelegateVar.Emailcontroler.setMessageBody_isHTML( NSSTR(PChar(FBody)), FisHTML );

//set attachement
for i := 0 to FFileAttachment.count-1 do
addAttachement(FFileAttachment[i]);

//send
MailDelegateVar.done := false;

//display the email
mainWindow.mainController.presentModalViewController_animated(MailDelegateVar.Emailcontroler,true);
//object passed to the presentModalViewController is no loonger needed
MailDelegateVar.Emailcontroler.Release;

//wait for email to compelte (or cancel)
while not MailDelegateVar.done do
Begin
Application.processmessages;
//while until finished
end;
tempresult := MailDelegateVar.EmailResult;
//Emailresult convert MFMailComposeResult to TEmailresult
case tempresult of
MFMailComposeResultCancelled : result := erCancelled;
MFMailComposeResultSaved : result := erSaved;
MFMailComposeResultSent : result := erSent;
MFMailComposeResultFailed : result := erFailed;
else result := erUknown;
end;
{$ENDIF}
end;

procedure Split(Delimiter: Char; S: string; ListOfStrings: TStrings) ;
Var I: integer;
begin
ListOfStrings.Clear;
while S<>'' do
begin
i := pos(Delimiter,S);
if I=0 then
Begin
//last part
ListOfStrings.Add(S);
exit;
end;
ListOfStrings.Add(copy(S,1,I-1));
S := Copy(S,I+1,Length(S));
end;

end;

function iosSendEmail( recTo,recCC,recBCC,subject,body : string; //comma delimited
FileAttachment : TstringList = nil;
isHTML: boolean = true ): TEmailresult;
var anEmail: TiOSEmail;
Begin
anEmail := TiOSEmail.create(nil);
try
Split(',',recTo,anEmail.RecipientsTo );
Split(',',recCC,anEmail.recipientsCC);
Split(',',recBCC,anEmail.recipientsBCC);
anEmail.subject := subject;
anEmail.body := body;
anEmail.isHTML := isHTML;
if FileAttachment<>nil then
anEmail.FileAttachment.text := FileAttachment.text;
result := anEmail.sendEmail;
finally
anEmail.free;
end;
end;

procedure Register;
begin
RegisterComponents('iOS', [TiOSEmail]);
end;

end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment