0

I am trying to send a message through Xcode programmatically but when I click the send button it opens the message sending box. But I want to send message directly without opening message box. Is this possible in iOS Xcode or not? Please help me.this is send button

when i click send button this is open sending message box

but don't want open sending message box.

the code is

- (IBAction)sendSMS:(id)sender 
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello  this is anup";
        controller.recipients = [NSArray arrayWithObjects:@"7026144009", 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(@"faild");
            UIAlertController *alrt=[UIAlertController alertControllerWithTitle:@"my apps" message:@"unknown error" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                //do something when click button
            }];
            [alrt addAction:okAction];
            break;
        }
        case MessageComposeResultSent:
            break;
        default:
            break;
    }

    [self dismissModalViewControllerAnimated:YES];
}
Michael Ozeryansky
  • 7,204
  • 4
  • 49
  • 61
Anup Gupta
  • 1,993
  • 2
  • 25
  • 40
  • 1
    Have you passed in this way for sending message..[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:+919999999999"]] – Uma Madhavi Feb 01 '16 at 07:33
  • 1
    then create a own view and get all user details and finally sent the details to backend server, the backend server trigger the sms to the particular number. – Anbu.Karthik Feb 01 '16 at 07:36
  • 3
    Possible duplicate of [How to programmatically send SMS on the iPhone?](http://stackoverflow.com/questions/10848/how-to-programmatically-send-sms-on-the-iphone) – Avi Feb 01 '16 at 07:39
  • @Anbu.Karthik can u explain me in details please. because i don't have much more knowledge. – Anup Gupta Feb 01 '16 at 11:47

3 Answers3

2

ya it is possible on that your scenario no need of MFMessageComposeViewController, just one webservice is enough, send the two (@"Hello this is anup",@"7026144009") to your backend , the backend developer send the SMS to that particular number using SMTP Server option.

Choice -2

if you want to handle in your own use some thirdparty SDK like skpsmtpmessage, it work like same SMTP.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

You can't programatically send a message without the user's content. The most you can do is open the messages app and if the user decides to send it, he can send it.

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
1

You can't programatically send a message without the user's consent. The most you can do, is open the messages app and if the user decides to send it, he can send it.

(IBAction)sendingMessage:(id)sender {

        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        if([MFMessageComposeViewController canSendText])
        {
            controller.body = @"SMS message here";
            controller.recipients = [NSArray arrayWithObjects:@"+919015156562", nil];
            controller.messageComposeDelegate = self;
            [self presentModalViewController:controller animated:YES];
        }

}

And:

(void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
    [self dismissViewControllerAnimated:YES completion:nil];
}
Ali Beadle
  • 4,486
  • 3
  • 30
  • 55