Introduction
I will demo some sample to present how to send SMS in iPhone programming.
Sample 1
The easiest way is this demo.
[[Uiapplication sharedapplication] Openurl: @ "SMS: 12345678"];
Html links: </P> <p> <a href = "SMS:" mce_href = "SMS: "> launch text application </a> <br/> <a href =" SMS: 1-408-555-1212 "mce_href =" SMS: 1-408-555-1212 "> New SMS message </a> </P> <p> native application URL strings: </P> <p> SMS: <br/> SMS: 1-408-555-1212 <br/>
Sample 2
We can use messageui framework and mfmessagecomposeviewcontroller to send SMS.
-(Ibaction) sendinreceivms :( ID) sender <br/>{< br/> mfmessagecomposeviewcontroller * controller = [[[mfmessagecomposeviewcontroller alloc] init] autorelease]; <br/> If ([mfmessagecomposeviewcontroller cansendtext]) <br/>{< br/> controller. body = @ "hello from mugunth"; <br/> controller. recipients = [nsarray arraywithobjects: @ "12345678", @ "87654321", nil]; <br/> controller. messagecomposedelegate = self; <br/> [self presentmodalviewcontroller: controller animated: Yes]; <br/>}< br/>}
You have to handle a callback method of mfmessagecomposeviewcontrollerdelegate so as to dismiss the modal View Controller.
-(Void) messagecomposeviewcontroller :( mfmessagecomposeviewcontroller *) controller didfinishwithresult :( messagecomposeresult) Result <br/>{< br/> switch (result) {<br/> case when: <br/> nslog (@ "cancelled"); <br/> break; <br/> case messagecomposeresultfailed: <br/> uialertview * Alert = [[uialertview alloc] initwithtitle: @ "MyApp" message: @ "Unknown error" <br/> delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; <br/> [alert show]; <br/> [alert release]; <br/> break; <br/> case messagecomposeresultsent: </P> <p> break; <br/> default: <br/> break; <br/>}</P> <p> [self dismissmodalviewcontrolleranimated: Yes]; <br/>}
Sample 3
This is a full sample code for mfmessagecomposeviewcontroller.
// Sms2viewcontroller. h <br/> // sms2 <br/> # import <uikit/uikit. h> <br/> # import <messageui/messageui. h> <br/> # import <messageui/mfmessagecomposeviewcontroller. h> <br/> @ interface sms2viewcontroller: uiviewcontroller <mfmessagecomposeviewcontrollerdelegate> <br/>{< br/> uilabel * message; <br/>}< br/> @ property (nonatomic, retain) uilabel * message; <br/>-(void) displaycomposersheet; <br/> @ end
// Sms2viewcontroller. m <br/> // sms2 <br/> # import "sms2viewcontroller. H "<br/> @ implementation sms2viewcontroller <br/> @ synthesize message; <br/>/* <br/> // The designated initializer. <br/> override to perform setup that is required before the view is loaded. <br/>-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil <br/>{< br/> If (Self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]) <br/>{< br/> // custom initialization <br/>}< br/> return self; <br/>}< br/> */<br/> // implement loadview to create a view hierarchy programmatically, without using a nib. <br/>/* <br/>-(void) loadview <br/>{< br/>}< br/> */<br/> // implement viewdidload to do additional setup after loading the view, typically from a nib. <br/>-(void) viewdidload <br/> {<br/> [Super viewdidload]; <br/> uibutton * smsbutton = [uibutton buttonwithtype: uibuttontyperoundedrect]; <br/> smsbutton. frame = cgrectmake (97.0, 301.0, 125.0, 37.0); <br/> smsbutton. adjustsimagewhendisabled = yes; <br/> [smsbutton settitle: @ "send SMS" forstate: uicontrolstatenormal]; <br/> [smsbutton settitlecolor: [uicolor colorwithwhite: 0.000 ALPHA: 1.000] forstate: uicontrolstatenormal]; <br/> [smsbutton settitleshadowcolor: [uicolor colorwithwhite: 0.000 ALPHA: 1.000] forstate: uicontrolstatenormal]; <br/> [smsbutton addtarget: Self action: @ selector (displaycomposersheet) forcontrolevents: uicontroleventtouchupinside]; <br/> message = [[uilabel alloc] initwithframe: cgrectmake (20.0, 360.0, 280.0, 29.0)]; <br/> message. frame = cgrectmake (20.0, 360.0, 280.0, 29.0); <br/> message. adjustsfontsizetofitwidth = yes; <br/> message. hidden = yes; <br/> message. TEXT = @ ""; <br/> message. userinteractionenabled = no; <br/> [self. view addsubview: smsbutton]; <br/> [self. view addsubview: Message]; <br/>}< br/>-(void) displaycomposersheet <br/>{< br/> mfmessagecomposeviewcontroller * picker = [[mfmessagecomposeviewcontroller alloc] init]; <br/> icker. messagecomposedelegate = self; <br/> picker. recipients = [nsarray arraywithobject: @ "123456789"]; <br/> // your recipient number or self for testing <br/> picker. body = @ "test from os4"; <br/> [self presentmodalviewcontroller: picker animated: Yes]; <br/> [picker release]; <br/> nslog (@ "SMS fired"); <br/>}< br/>-(void) messagecomposeviewcontroller :( mfmessagecomposeviewcontroller *) controller didfinishwithresult :( messagecomposeresult) result <br/>{< br/> message. hidden = no;/<br/> switch (result) <br/>{< br/> case messagecomposeresultcancelled: <br/> message. TEXT = @ "Result: canceled"; <br/> nslog (@ "Result: canceled"); <br/> break; <br/> case messagecomposeresultsent: <br/> message. TEXT = @ "Result: sent"; <br/> nslog (@ "Result: sent"); <br/> break; <br/> case messagecomposeresultfailed: <br/> message. TEXT = @ "result: Failed"; <br/> nslog (@ "result: Failed"); <br/> break; <br/> default: <br/> message. TEXT = @ "Result: not sent"; <br/> nslog (@ "Result: not sent"); <br/> break; <br/>}< br/> [self dismissmodalviewcontrolleranimated: Yes]; <br/>}< br/> // override to allow orientations other than the default portrait orientation. <br/>-(bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation <br/>{< br/> return yes; <br/>}< br/>-(void) didreceivememorywarning <br/>{< br/> // releases the view if it doesn' t have a superview. <br/> [Super didreceivememorywarning]; <br/> // release any cached data, images, etc that aren't in use. <br/>}< br/>-(void) viewdidunload <br/>{< br/> // release any retained subviews of the main view. <br/> // e.g. self. myoutlet = nil; <br/>}< br/>-(void) dealloc <br/>{< br/> [Super dealloc]; <br/>}< br/> @ end