我已经看过几个发送短信的教程,但都打开了原生iOS消息应用程序,我想要的是我的应用程序发送它而不更改向用户发送不可见的应用程序。
这是我正在使用的代码但打开本机应用程序:
#pragma mark - IBAction methods
-(IBAction)sendSMS:(id)sender {
//check if the device can send text messages
if(![MFMessageComposeViewController canSendText]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Advertencia" message:@"Su dispositivo no permite el envio de mensajes SMS" delegate:nil cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
[alert show];
return;
}
//set receipients - telefonos
NSArray *recipients = [NSArray arrayWithObjects:textNumero.text, nil];
NSLog(@"telefono: %@",recipients);
//set message text
NSString * message = textMensaje.text;
NSLog(@"mensaje: %@",recipients);
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipients];
[messageController setBody:message];
// Present message view controller on screen
[self presentViewController:messageController animated:YES completion:nil];
}
#pragma mark - MFMailComposeViewControllerDelegate methods
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Advertencia" message:@"Ha ocurrido un error en el envio del mensaje" delegate:nil cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}