Search This Blog

Jul 29, 2011

How to call, sms and send email from iPhone SDK ?

This code shows how to call using iphone sdk and send email or sms.


You can download source code from here Download.

1. Code for .h file.

#import
#import
#import

@interface MessageViewController : UIViewController

{

}

- (IBAction)btnEmail_Clicked:(id)sender;
- (IBAction)btnMessage_Clicked:(id)sender;
- (IBAction)btnCall_Clicked:(id)sender;

@end


2. Code for .m file.

#import "MessageViewController.h"


@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.title = @"Message sending";
}

- (IBAction)btnEmail_Clicked:(id)sender
{
    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setToRecipients:[NSArray arrayWithObject:@"user@gmail.com"]];
    [controller setSubject:@"iPhone Email Example Mail"];
    [controller setMessageBody:@"http://iphoneapp-dev.blogspot.com" isHTML:NO];
    [self presentModalViewController:controller animated:YES];
    [controller release];
}

- (IBAction)btnMessage_Clicked:(id)sender
{
    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello Friends this is sample text message.";
        controller.recipients = [NSArray arrayWithObjects:@"+919999999999", nil];
        controller.messageComposeDelegate = self;
        [self presentModalViewController:controller animated:YES];
    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
  
    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"Cancelled");
            break;
        case MessageComposeResultFailed:
            NSLog(@"Failed");
            break;
        case MessageComposeResultSent:
            NSLog(@"Send");
            break;
        default:
            break;
    }
  
    [self dismissModalViewControllerAnimated:YES];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
  
    if (result == MFMailComposeResultSent) {
        NSLog(@"It's away!");
    }
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)btnCall_Clicked:(id)sender
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"+919999999999"]];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
  
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

5 comments:

  1. All the post are really informative and helped me a lot!!
    But i m not able to download a source code.
    Other thing i want is it possible to send mail via simulator???
    If yes then kindly suggest me.

    Regards,
    Jagruti

    ReplyDelete
  2. How abt sending SMS for free to any mobile number?
    Check this out: https://itunes.apple.com/us/app/smsglobal/id570193291?mt=8

    ReplyDelete
  3. nice work but r u sure ur call method can make call from iphone .... i think u need to add some update code... plz check and update ...

    ReplyDelete