[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[MessageUI] 002 test email composer

"Check the API is loaded"
(Smalltalk includesKey: #MFMailComposeViewController) ifFalse: [
	(SwikiCodeRobot @ #api) process: #s8-media tagged: #MessageUI
].


"Implementation of email UI for testing"
UIViewController
	subclass: #MFMailTestViewController
	instanceVariableNames: '' classVariableNames: ''
	poolDictionaries: #MFMailComposeResult
	category: #MFMTest!

! MFMailTestViewController class methodsFor: #example !
example
	"Send email to smalltalking list."

	MFMailComposeViewController canSendMail ifFalse: [
		^self alert: 'This device can''t send emails.'
	].
	MFMailTestViewController instance
		openOnTop;
		send: 'This is an email sent from S8 system running iOS.'
		subject: 'Please ignore (it is a test email from coco8)'
		to: '[email protected]'! !

! MFMailTestViewController class methodsFor: #objC !
implementation
	" Private - Return the implementation literal of the receiver. "

	^(#outlets -> #())
	,(#methods -> #(
		#(#mailComposeViewController:didFinishWithResult:error: 'void id int id')
		))! !

! MFMailTestViewController methodsFor: #ui !
send: body subject: subject to: destination
	" Open UI to send email to destination. "

	| controller |
	destination isString ifTrue: [
		^self	send: body subject: subject
			to: (destination asArrayOfSubstringsSeparatedBy: $,)
	].
	controller := MFMailComposeViewController instance.
	controller
		setMessageBody: body isHTML: false; setSubject: subject;
		setToRecipients: destination asArray;
		mailComposeDelegate: self;
		yourself.
	[	"this operation must be deferred enough time to let UI be ready to compose contents"
		self presentViewController: controller animated: true completion: nil
	] valueDeferred: 1000.! !

! MFMailTestViewController methodsFor: #objC !
mailComposeViewController: hController didFinishWithResult: result error: pError
	" Private - The user finished composing the email. "

	<objC: method>
	self dismissViewControllerAnimated: true completion: nil.
	self navigationController popToRootViewControllerAnimated: true.
	result = MFMailComposeResultSent ifTrue: [ ^self print: 'Email sent' ].
	result = MFMailComposeResultSaved ifTrue: [ ^self print: 'Email copy saved as draft' ].
	result = MFMailComposeResultCancelled ifTrue: [ ^self print: 'Email cancelled' ].
	result = MFMailComposeResultFailed ifTrue: [ ^self print: 'Failure occurred when trying to compose this email' ].
	^self print: 'An error occurred when trying to compose this email. result=',result toString! !


"Sending the email to smalltalking list"
	MFMailTestViewController example