Wednesday, May 4, 2011

Code for Sending SMS from iPhone Application

Hello Friends,

Today we will learn to send sms from your iPhone application. As always Apple has provided ready to use frame work for sending sms from your application. Writing a code for sending sms as easy as sending a sms from iPhone. To implement this functionality let's start quick tutorial.

  • Add frame work in your application named "MessageUI.framework".
  • Import<UIKit/UIKit.h> , <MessageUI/MessageUI.h> ,  <MessageUI/MFMailComposeViewController.h> files in header file and write the following code in header file (.h file).

@interface MessageComposerViewController : UIViewController < MFMessageComposeViewControllerDelegate> {
      
       IBOutlet UILabel *feedbackMsg;
}

@property (nonatomic, retain) IBOutlet UILabel *feedbackMsg;

-(IBAction)showSMSPicker:(id)sender;
-(void)displaySMSComposerSheet;

@end

  • Add the following code in implementation file (.m file).
Following code will check device configuration and will throw error if it is not configured properly.

-(IBAction)showSMSPicker:(id)sender {
//     The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later.
//     So, we must verify the existence of the above class and log an error message for devices
//       running earlier versions of the iPhone OS. Set feedbackMsg if device doesn't support
//       MFMessageComposeViewController API.
       Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
      
       if (messageClass != nil) {                          
          // Check whether the current device is configured for sending SMS messages
          if ([messageClass canSendText]) {
                   [self displaySMSComposerSheet];
          }
          else {         
                   feedbackMsg.hidden = NO;
                   feedbackMsg.text = @"Device not configured to send SMS.";

          }
       }
       else {
          feedbackMsg.hidden = NO;
          feedbackMsg.text = @"Device not configured to send SMS.";
       }
}

       //IF device is configure successfully then below code will run.

          // Displays an SMS composition interface inside the application.
-(void)displaySMSComposerSheet
{
       MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
       picker.messageComposeDelegate = self;
      
       [self presentModalViewController:picker animated:YES];
       [picker release];
}


// Dismisses the message composition interface when users tap Cancel or Send. Proceeds to update the
// feedback message field with the result of the operation.

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                     didFinishWithResult:(MessageComposeResult)result {
      
       feedbackMsg.hidden = NO;
       // Notifies users about errors associated with the interface
       switch (result)
       {
          case MessageComposeResultCancelled:
                   feedbackMsg.text = @"Result: SMS sending canceled";
                   break;
          case MessageComposeResultSent:
                   feedbackMsg.text = @"Result: SMS sent";
                   break;
          case MessageComposeResultFailed:
                   feedbackMsg.text = @"Result: SMS sending failed";
                   break;
          default:
                   feedbackMsg.text = @"Result: SMS not sent";
                   break;
       }
       [self dismissModalViewControllerAnimated:YES];
}







2 comments:

  1. hello Sir,
    I need some information can u please explain me.Can't we send SMS without the message body means directly by clicking the button and please can u tell me how to read the sms sent by another number in my application is it possible or not.

    thank u..

    ReplyDelete