Using MFMessageComposeViewController to Send to an Array of Numbers Individually - ios

Currently I am trying to send sms from a users phone through an app.
The idea is that the user selects the recipients of the message and then one by one the message is sent to each recipient.
Currently this is how I am doing it after a button is clicked this happens
if (self.abNumbersSelected.count > 0){
for(NSString *phoneNumber in self.abNumbersSelected)
{
if(self.currentController)
[self.currentController dismissViewControllerAnimated:YES completion:^{[self sendInviteMessage:phoneNumber];}
];
else
[self sendInviteMessage:phoneNumber];// message that invites selected message
}
}
abNumbersSelected is an array of numbers that the user has picked the sendInviteMessage is here:
- (void)sendInviteMessage:(NSString*)invitingNumber
{
//Invites the numbers using sms
//First checks if it duplicates it
if ([self checkDuplicate:invitingNumber]) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Invalid Phone Number" message:#"The phone number you entered is not valid." delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
self.phoneField.text = #"";
}
//if not a duplicate then it sends the message
else {
if (SharedSessionManager.authToken) {
NSString* currentUserName = #"";
if (SharedSessionManager.currentUser.firstName && SharedSessionManager.currentUser.lastName) {
currentUserName = [NSString stringWithFormat:#"%# %#", SharedSessionManager.currentUser.firstName, SharedSessionManager.currentUser.lastName];
}
else if (SharedSessionManager.currentUser.firstName) {
currentUserName = SharedSessionManager.currentUser.firstName;
}
if (!customLoadingView.isLoading)
[customLoadingView beginLoading];
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
NSString *message = [NSString stringWithFormat:#"%# invited you to join Friendli - a mobile, location based social network. Join now at friendli.co",currentUserName];
controller.body = message;
controller.recipients = [NSArray arrayWithObjects:invitingNumber, nil];
controller.messageComposeDelegate = self;
controller.disableUserAttachments;
controller.title = #"Invite Friend";
self.currentController = controller;
[self presentModalViewController:self.currentController animated:YES];
}
}
}
}
Currently no message shows up to be sent.
Also here is the messageComposeViewController: method
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
In the log this is what is said when 2 recipients are picked and the button to trigger the MFMessageComposeViewController to be created is clicked by the user
2015-06-05 13:29:10:521 Friendli[241:807] [self.abNumbersSelected count]: 2
2015-06-05 13:29:11:638 Friendli[241:807] Reading auth token from keychain. (friendli.116.authtoken)
2015-06-05 13:29:11:643 Friendli[241:807] Reading auth token from keychain. (friendli.116.authtoken)
2015-06-05 13:29:12.869 Friendli[241:10525] Remote compose controller timed out (NO)!

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{
if (self.abNumbersSelected.count > 0 && self.multipleRecipients) {
[self sendInviteMessage:[self.abNumbersSelected objectAtIndex:0]];
}
}];
}
In the didFinish method for the MFMessageComposeController in the dismiss statement using the if statement in the block of code the code will continue to create more MFMessageComposeViewControllers till it is done.

Related

Mail Controller in iOS keeps on getting "cancelled"

Only the email-subject is getting set to "Test mail" and recipients remain empty. MailController opens for a while and gives the alert as "Message Cancelled".
Anyone please help me out!
- (IBAction)sendEmail:(id)sender { //This is a button to send E-mail
mailController=[[MFMailComposeViewController alloc]init];
NSString *emailBody = #"Test mail from Fortune";
[mailController setToRecipients:[NSArray arrayWithObjects:#"hi#fortune.com",#"hello#fortune.com", nil]];
[mailController setCcRecipients:#[#"gthg65#gmail.com"]];
[mailController setBccRecipients:#[#"resumes#fortune.com"]];
[mailController setMessageBody:emailBody isHTML:NO];
[mailController setSubject:#"Test mail "];
mailController.mailComposeDelegate=self;
[self presentViewController:mailController animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *messageResult;
if (error!=nil)
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Mail Error" message:[error localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else{
switch (result) {
case MFMailComposeResultCancelled:
messageResult=#"Mail Cancelled";
break;
case MFMailComposeResultFailed:
messageResult=#"Mail Failed";
break;
case MFMailComposeResultSaved:
messageResult=#"Mail Saved";
break;
case MFMailComposeResultSent:
messageResult=#"Mail Sent";
break;
default:
break;
}
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Mail Result" message:messageResult delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Works on a test device. Also Apple recently changed their developer account memberships and you can register for a free apple developer account and run the app on a test device that way.

Can you send sms without using the native ios application?

I have seen several tutorials to send SMS messages but all open the native iOS message application and what I want is that my application send it without changing application that is sending something invisible to the user.
This is the code I'm using but opens the native application:
#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];
}

iPhone not sending text programmatically

I made a basic app that for now just has a button that you click, and it brings up the sms composer (ios7 and xcode 5). I think I've handled everything well. The simulator doesn't support sending messages, so I tried on my phone, but the message never actually sends. You can click the send button and cancel button fine, but again, the message never sends. Any ideas? Here is my code:
- (IBAction)text:(UIButton *)sender {
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
[messageVC setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
NSString *smsString = [NSString stringWithFormat:#"message to send"];
messageVC.body = smsString;
messageVC.recipients = #[#"number to send to..."];
messageVC.messageComposeDelegate = self;
[self presentViewController:messageVC animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
NSLog(#"Message was cancelled");
[self dismissViewControllerAnimated:YES completion:NULL]; break;
case MessageComposeResultFailed:
NSLog(#"Message failed");
[self dismissViewControllerAnimated:YES completion:NULL]; break;
case MessageComposeResultSent:
NSLog(#"Message was sent");
[self dismissViewControllerAnimated:YES completion:NULL]; break;
default:
break;
}
}
EDIT: So I tried it this morning (I did nothing to it overnight) and it worked. Not sure what the issue was. Thanks though!
I assume the messageVC.recipients is in correct format (array of numbers)?
Try:
//check if the device can send text messages
if(![MFMessageComposeViewController canSendText]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device cannot send text messages" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
//set receipients
NSArray *recipients = [NSArray arrayWithObjects:#"0912345679",#"0923456790",#"0934567901", nil];
//set message text
NSString * message = #"this is a test sms message.";
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];
Try This In Your MessageCompose Delegate:
(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Message was cancelled");
break;
case MessageComposeResultFailed:
NSLog(#"Message failed");
break;
case MessageComposeResultSent:
NSLog(#"Message was sent");
break;
default:
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}

Texting contacts within an app

I've developed an app that allows the user to text contacts that they add in the app. The problem I'm having is it seems that if the user already exists in the person's native iOS address book, the text will send no problem. But if the contact exists only within the app, the text will not go through. Has anyone else experienced something like this before?
EDIT: Code below
if([MFMessageComposeViewController canSendText])
{
controller.body = #"";
controller.recipients = arrayContactMobileStrings;
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Message not sent", #"") message:NSLocalizedString(#"Error sending message", #"")
delegate:self cancelButtonTitle:NSLocalizedString(#"OK", #"") otherButtonTitles: nil];
switch (result)
{
case MessageComposeResultCancelled:
[alert show];
break;
case MessageComposeResultFailed:
[alert show];
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

how to send an email from within the app

I'm trying to send an email with data from text field and image and it doesn't work, please advise. Here is my code:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)
{
Cocktails*c=[[Cocktails alloc]init];
_arrTextField=[NSArray arrayWithObjects:_txtName,_txtIngredients,_txtPreper,_txtServe,_txtFrom ,nil];
NSLog(#"send email");
if ([MFMailComposeViewController canSendMail])
{
NSMutableArray*recipients=[[NSMutableArray alloc]init];
[recipients addObject:#"maya1580#gmail.com"];
MFMailComposeViewController *controller= [[MFMailComposeViewController alloc]init];
controller.mailComposeDelegate= self;
[controller addAttachmentData:_imgDrink mimeType:#"image/png" fileName:#"Myimage"];
[controller setSubject:#"my cocktail"];
[controller setMessageBody: _arrTextField isHTML:NO];
[controller setToRecipients:recipients];
}
else
{
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:#"Your devise is not set up for Email" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:Nil, nil];
[alert show];
// [alert release];
}
You never present the mail controller. It needs to be presented like any other modal view controller.
Here that should help:
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(#"Mail not sent.");
break;
}
// Remove the mail view
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction)sendCode:(id)sender {
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Your subject"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"maya1580#gmail.com", nil];
[mailer setToRecipients:toRecipients];
UIImage *myImage = [UIImage imageNamed:#"MyImage.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"myimag
e"];
NSString *emailBody =
#"your body";
[mailer setMessageBody:emailBody isHTML:NO];
[mailer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:mailer animated:YES];
}
else {
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Your device doesnt support that action."
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert2 show];
}
}
}
and in the .h file add:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface Controller : UIViewController <MFMailComposeViewControllerDelegate> {
}
-(IBAction)send:(id)sender;
#end
You cant send automaticaly email because apple wont let you do this kind of things without user knowledge. You must present presentModalView!

Resources