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

test[MessageUI] 001 test SMS composer

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


"Implementation of SMS UI for testing"
UIViewController
	subclass: #MFMessageTestViewController
	instanceVariableNames: '' classVariableNames: ''
	poolDictionaries: #MFMessageComposeResult
	category: #MFMTest!

! MFMessageTestViewController class methodsFor: #example !
example
	" Send a SMS to phone 123456789 "

	MFMessageComposeViewController canSendText ifFalse: [
		^self alert: 'This device can''t send SMS.'
	].
	MFMessageTestViewController instance
		openOnTop;
		send: 'This is a SMS test from S8'
		to: #123456789! !

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

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

! MFMessageTestViewController methodsFor: #ui !
send: bodyOfMessage to: destination
	" Open UI to send a SMS to destination. "

	| controller |
	destination isString ifTrue: [
		^self	send: bodyOfMessage
			to: (destination asArrayOfSubstringsSeparatedBy: $,)
	].
	controller := MFMessageComposeViewController instance.
	controller
		body: bodyOfMessage;    
		recipients: destination asArray;
		messageComposeDelegate: 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.! !

! MFMessageTestViewController methodsFor: #objC !
messageComposeViewController: hController didFinishWithResult: result
	" Private - The user finished composing the message. "

	<objC: method>
	self dismissViewControllerAnimated: false completion: nil.
	self navigationController popToRootViewControllerAnimated: true.
	result = MessageComposeResultCancelled ifTrue: [ ^self print: 'Message cancelled' ].
	result = MessageComposeResultSent ifTrue: [ ^self print: 'Message sent' ].
	^self print: 'Message result=',result toString! !


"Sending the SMS to 123456789"
	MFMessageTestViewController  example