0

I am writing iphone a app, in which I need to send email and sms at some interval automatically to a number and email ID. But everytime it goes to that part after some interval, a pop-up windows comes for both email and sms, and when I press send button then only it sends the email or sms.

How can I make it automatic, so that I dont have to press the send button again and again and it does the action automatically. I found this piece of code for sending email and sms and I am using this code only for my purpose. How can I modify it to make this automatic.

//
//  MessageViewController.m
//  EmailExample
//
//  Copyright http://iphoneapp-dev.blogspot.com. All rights reserved.
//

#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
Akansha
  • 47
  • 1
  • 11
  • You can't. Closest you will get is sending information to a REST web service, and this will only work when your app is running. – danielbeard Jun 19 '12 at 08:09
  • Possible duplicate of http://stackoverflow.com/questions/2827014/iphone-sdk-send-email-in-background-in-iphone-app – Mitesh Khatri Jun 19 '12 at 08:12

2 Answers2

3

You can't, if this where possible you could send some SMS to a premium service and the user could end up with a high phone bill. That's why Apple doesn't allow this.

You might be able to do it if you sen the SMS/e-mail via a server or in the case of e-mail directly via SMTP.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
0
// Build request
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];
NSURL *url = [NSURL URLWithString:urlString];


ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request setPostFormat:NSUTF8StringEncoding];
[request setPostValue:kFromNumber forKey:@"From"];
[request setPostValue:kToNumber forKey:@"To"];
[request setPostValue:kMessage forKey:@"Body"];

[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestFail:)];

hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText = @"Please wait...";
hud.detailsLabelText = @"Sending message";
hud.delegate = self;
hud.square = NO;
hud.dimBackground = YES;

[request startAsynchronous];

You can use this code and change parameters as per your needs i hope this will help you in sending SMS.

AHSAN NAZIR RAJA
  • 166
  • 1
  • 13
  • ASIFormDataRequest is not a built in function for iOS development, therefore this answer is incomplete as it does not give the steps to install the required packages and list which #inlcude files to include. – TimeHorse May 17 '20 at 18:59