sending message using MFMessageComposeViewController failed - ios

I am facing one issue for send message to multiple contacts :
301 up contacts : Message app is not opening, If we click invite on multiple time then iMessage is opening but it will take more time (2 minute) to load message
351 up contacts : Message is open with black new message screen then come back to our application with out opening message screen.
Here is my code:
contacts is the array of phone number
NSMutableArray *contacts = [NSMutableArray array];
for (User *user in users) {
if (user.phone.length) {
NSString *strphonenumber = [NSString stringWithFormat:#"%#",user.phone];
[contacts addObject:strphonenumber];
}
}
MFMessageComposeViewController *messanger = [[MFMessageComposeViewController alloc]init];
messanger.messageComposeDelegate = self;
messanger.recipients = contacts;
messanger.body = [NSString stringWithFormat:#“body”;
[self presentViewController:messanger animated:YES completion:NULL];
I am getting this error:
<CKSMSComposeRemoteViewController: 0x12802f810> timed out waiting for fence barrier from com.apple.mobilesms.compose

Try this...
NSString *strContacts = [NSString stringWithFormat:#"%#",add multipal contacts];
MFMessageComposeViewController *message = [[MFMessageComposeViewController new];
message.recipients = #[strContacts];

I to had the same problem then figured
messanger.recipients = // should always be an array of strings.
Make sure the phone numbers you send to messanger.recipients are NSString.

this works for me:
set delegate to interface:
#interface ViewController <MFMessageComposeViewControllerDelegate>{}
check if device is able to send message
if([MFMessageComposeViewController canSendText] ){
//device is possible to send messages
}else{
//device can't send messages
}
prepare message:
MFMessageComposeViewController* comp = [[MFMessageComposeViewController alloc] init];
//set properties
comp.body = #"body";
comp.recipients = [NSArray arrayWithObjects:phone1, phone2, nil];
comp.messageComposeDelegate = self;
open dialog:
[self presentViewController:comp animated:YES completion:nil];
determine result
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
//test result
switch (result) {
case MessageComposeResultCancelled:
[self makeAlert:#"Result canceled"];
break;
//message was sent
case MessageComposeResultSent:
[self makeAlert:#"Result sent"];
break;
case MessageComposeResultFailed:
[self makeAlert:#"Result Failed"];
break;
default:
break;
}
//dismiss view
[self dismissViewControllerAnimated:YES completion:nil];
}

Just use this method:
- (IBAction)sendSMS:(id)sender {
MFMessageComposeViewController *controller =
[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText]){
controller.body = #"Hello";
controller.recipients = [NSArray arrayWithObjects:#"12345678",#"87654321", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
Callback method:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
switch (result) {
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(#"Error occured");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

Related

MFMailComposeViewController delegate not called

I'm not able to call Mail composer delegate method even if I'm writing below code:
-(void)openMailComposerInViewController:(UIViewController *)hostController{
if ([MFMailComposeViewController canSendMail])
{
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"Test Email";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"support#test.com"];
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:emailTitle];
[mailComposer setMessageBody:messageBody isHTML:NO];
[mailComposer setToRecipients:toRecipents];
// Present mail view controller on screen
[hostController presentViewController:mailComposer animated:YES completion:NULL];
mailController = hostController;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
pragma mark - Mail
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[mailController dismissViewControllerAnimated:YES completion:NULL];
}
I do not want to call mail composer on self
However, my delegate is not getting called. Please help. The application crashed on clicking on Cancel or Send mail button
I experienced this problem, and my problem was I had the following code:
self.mailComposer.mailComposeDelegate = self
self.mailComposer = MFMailComposeViewController()
I was setting the delegate before I initialized the mailComposer object. I just switched those 2 lines to this and it worked:
self.mailComposer = MFMailComposeViewController()
self.mailComposer.mailComposeDelegate = self
Because of ARC, I was losing the viewcontroller in which the delegate was written so it was not gettting called. Below code helped me fix the issue:
+(MailComposer *)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMailComposer = [[MailComposer alloc]init];
});
return sharedMailComposer;
}
Then I used the above instance while calling the MailComposerViewController
[[MailComposer sharedInstance] openMailComposerInViewController:[(UINavigationController *)moduleRouter.container topViewController]];
It Worked :)
your class instance must be same for setMailComposeDelegate and presentViewController.
mailComposer.mailComposeDelegate = hostController;
[hostController presentViewController:mailComposer animated: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];
}

How to send email with UITableView

So I'm using this array to display email addresses in a custom table cell
EmailAddress = [NSArray arrayWithObjects:#"sample#aths.ac.ae",:#"sample#aths.ac.ae",:#"sample#aths.ac.ae", :#"sample#aths.ac.ae",:#"sample#aths.ac.ae",:#"sample#aths.ac.ae", nil];
and I used this code for my email but I always get a SIGABRT error whenever I press the cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [EmailAddress objectAtIndex:indexPath.row];
[controller setToRecipients:toRecipients];
[controller setTitle:#""];
[controller setSubject:#""];
[controller setMessageBody:#"" isHTML:NO];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
controller.modalPresentationStyle = UIModalPresentationFormSheet;
}
else
{
controller.modalPresentationStyle = UIModalPresentationFullScreen;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
NSString *strMailResult;
switch (result)
{
case MFMailComposeResultCancelled:
strMailResult = NSLocalizedString(#"E-Mail Cancelled",#"");
break;
case MFMailComposeResultSaved:
strMailResult = NSLocalizedString(#"E-Mail Saved",#"");
break;
case MFMailComposeResultSent:
strMailResult = NSLocalizedString(#"E-Mail Sent",#"");
break;
case MFMailComposeResultFailed:
strMailResult = NSLocalizedString(#"E-Mail Failed",#"");
break;
default:
strMailResult = NSLocalizedString(#"E-Mail Not Sent",#"");
break;
}
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Message",#"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alertView show];
[self dismissViewControllerAnimated:YES completion:nil];
}
but I end up with the error I think the error is from the toRecipient but I dont know how to fix it.
One obvious problem is with the following two lines:
NSArray *toRecipients = [EmailAddress objectAtIndex:indexPath.row];
[controller setToRecipients:toRecipients];
It should be:
NSString *toRecipient = [EmailAddress objectAtIndex:indexPath.row];
[controller setToRecipients:#[ toRecipient ]];
since you only get a single recipient value from the EmailAddress array.

MFMailComposeViewController doesn't close

I have an app where I use E-Mail to developer Button. When I press the button VEMailView-controller opens.
This is just wrapper for MFMailComposeViewController.
When I press "send" button, the controller have to be closed but I see just white window. No more. It must be closed to main ViewController. How to fix?
This is my code:
#import <MessageUI/MessageUI.h>
#import "VEMailView.h"
#interface VEMailView () <
MFMailComposeViewControllerDelegate,
UINavigationControllerDelegate
>
// UILabel for displaying the result of the sending the message.
#end
#implementation VEMailView{
BOOL alreadyOpened;
}
- (void)viewDidLoad
{
[super viewDidLoad];
alreadyOpened = NO;
}
- (void) viewDidAppear:(BOOL)animated {
[self showMailPicker];
}
- (void)showMailPicker
{
if ([MFMailComposeViewController canSendMail])
{
[self displayMailComposerSheet];
}
}
- (void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"iOS"];
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
[picker setToRecipients:toRecipients];
NSString *emailBody = #" ";
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Result: Mail sending canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Result: Mail sending failed");
break;
default:
NSLog(#"Result: Mail not sent");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
That's because you are making an extra view controller. Create the MFMailComposeViewController in the view controller where the button is. The mail compose controller is a controller by itself. There is a white screen because that is the default view of YOUR VEMailView. Get rid of that and put these methods:
- (void)showMailPicker;
- (void)displayMailComposerSheet;
in the view controller with the button. Also make it the delegate.
I found my own solution.
To solve this problem change
[self dismissViewControllerAnimated:YES completion:nil];
into
[self dismissViewControllerAnimated:YES completion:^{[self dismissViewControllerAnimated:YES completion:nil];}];
Thanks Michal for idea.

Send Email from TableView in ViewController

I have a TableView set to trigger a different ViewController when the user selects an item in the list, but I would like "Contact Us" to trigger the Messages Framework (In App Email) so the user can send an email and not get pushed to a ViewController.
Any suggestions? Below is a block of code that I'm using for "Contact Us".
Sorry if this is an obvious answer, learning as I go and envy all you wizards out there!
NSMutableDictionary *sectionContactUs = [NSMutableDictionary dictionary];
[sectionContactUs setObject:kSlideViewControllerSectionTitleNoTitle forKey:kSlideViewControllerSectionTitleKey];
[sectionContactUs setObject:#"Contact Us" forKey:kSlideViewControllerSectionTitleKey];
NSMutableDictionary *contactUsViewControllerDictionary = [NSMutableDictionary dictionary];
[contactUsViewControllerDictionary setObject:#"Contact Us" forKey:kSlideViewControllerViewControllerTitleKey];
[contactUsViewControllerDictionary setObject:#"ContactUsViewController" forKey:kSlideViewControllerViewControllerNibNameKey];
[contactUsViewControllerDictionary setObject:[ContactUsViewController class] forKey:kSlideViewControllerViewControllerClassKey];
[sectionTest setObject:[NSArray arrayWithObject:contactUsViewControllerDictionary] forKey:kSlideViewControllerSectionViewControllersKey];
[datasource addObject:sectionContactUs];
Use MFMailComposeComposeView:
First Import MessageUI framework in your project
And write #import <MessageUI/MessageUI.h> in your view controller also add MFMailComposeViewControllerDelegate at interface
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:#"test1#gmail.com", #"test2#gmail.com", nil];
[controller setToRecipients:toRecipients];
[controller setTitle:#"Contact Us"];
[controller setSubject:#"Your Mail Subject"];
[controller setMessageBody:#"Mail body here \n Comments" isHTML:NO];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
controller.modalPresentationStyle = UIModalPresentationFormSheet;
}
else
{
controller.modalPresentationStyle = UIModalPresentationFullScreen;
}
[self presentModalViewController:controller animated:YES];
Also add this methods
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
NSString *strMailResult;
switch (result)
{
case MFMailComposeResultCancelled:
strMailResult = NSLocalizedString(#"E-Mail Cancelled",#"");
break;
case MFMailComposeResultSaved:
strMailResult = NSLocalizedString(#"E-Mail Saved",#"");
break;
case MFMailComposeResultSent:
strMailResult = NSLocalizedString(#"E-Mail Sent",#"");
break;
case MFMailComposeResultFailed:
strMailResult = NSLocalizedString(#"E-Mail Failed",#"");
break;
default:
strMailResult = NSLocalizedString(#"E-Mail Not Sent",#"");
break;
}
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Message",#"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alertView show];
[self dismissModalViewControllerAnimated:YES];
}
you can open MFMailComposeViewController for send mail..
-(void)doEmail
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate =self;
[picker setMessageBody:#"Body message" isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
//////nslog (#"mail finished");
}

Resources