Mail Controller in iOS keeps on getting "cancelled" - ios

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.

Related

Using MFMessageComposeViewController to Send to an Array of Numbers Individually

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.

Mail Compose not dismiss when click cancel button

I have this code who send a e-mail to users, Im using MFMailCompose:
.h file
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface TestViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
#property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;
.m file
#synthesize mailComposeDelegate
-(void)sendEmail:(NSString*)valor{
//Valor receive the email
NSString *corpoMensagem = #"No have body yet...";
// From within your active view controller
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self;// Required to invoke mailComposeController when send
[mailCont setSubject:#"FazerBem - Recibo de Compra"];
[mailCont setToRecipients:[NSArray arrayWithObject:valor]];
[mailCont setMessageBody:corpoMensagem isHTML:YES];
[self presentViewController:mailCont animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
if (error){
NSString *errorTitle = #"Erro to send";
NSString *errorDescription = [error localizedDescription];
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:errorTitle message:errorDescription delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[errorView show];
}
[self becomeFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
}
when I click the send button he can send the email to the recipient and can close the screen, now when I click the cancel button, it opens 2 more options 'delete draft' and 'save draft' when I click on one of the app crashes and returns me the following error:
[TestViewController respondsToSelector:]: message sent to deallocated instance 0x16b3b470
How I can solve this problem?
Use Switch case to for performing actions:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
UIAlertView *alert;
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
alert = [[UIAlertView alloc] initWithTitle:#"Draft Saved" message:#"Composed Mail is saved in draft." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
case MFMailComposeResultSent:
alert = [[UIAlertView alloc] initWithTitle:#"Success" message:#"You have successfully referred your friends." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
case MFMailComposeResultFailed:
alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"Sorry! Failed to send." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:nil];
}
Remove this line:
[self becomeFirstResponder];
That should fix your problem.
If above solution didn't work for you as it was for me, here, instead of self use instance of MFMailComposeViewController:
Instead Of
[self dismissViewControllerAnimated:YES completion:nil];
Use
[mailComposer 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];
}

MFMailComposeViewController black screen on iPad

I'm using cocos2d-x framework and I need to popup the mail when clicking button.
The code works fine on iphone5 (6.0), ipod touch 5(6.0):
MailSender.h
#interface MailSender : UIViewController <MFMailComposeViewControllerDelegate>
{
UIViewController *currentModalViewController;
}
-(void)sendMail:(const char *)subject receiver:(const char *)receiver;
#end
MailSender.mm
#import "MailSender.h"
#import "../cocos2dx/platform/ios/EAGLView.h"
#implementation MailSender
- (void)sendMail:(const char *)subject receiver:(const char *)receiver
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithUTF8String:subject]];
NSArray *toRecipients = [NSArray arrayWithObject:[NSString stringWithFormat:#"%s", receiver]];
[mailer setToRecipients: toRecipients];
//NSString *emailBody = [NSString stringWithFormat:#"<p>This is a sample posting in iOS. My Score is %s!</p>",score];
NSString *emailBody = #"";
[mailer setMessageBody:emailBody isHTML:YES];
// only for iPad
// mailer.modalPresentationStyle = UIModalPresentationFormSheet;
UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
currentModalViewController = [UIViewController alloc];
[rootViewController.view addSubview:currentModalViewController.view];
[currentModalViewController presentViewController:mailer animated:true completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
const char *message;
switch (result)
{
case MFMailComposeResultCancelled:
message = "Mail cancelled";
break;
case MFMailComposeResultSaved:
message = "Mail saved";
break;
case MFMailComposeResultSent:
message = "Mail send";
break;
case MFMailComposeResultFailed:
message = "Mail failed";
break;
default:
message = "Mail cancelled";
break;
}
NSLog(#"%s",message);
[currentModalViewController dismissViewControllerAnimated:true completion:nil];
[currentModalViewController.view.superview removeFromSuperview];
[currentModalViewController release];
}
#end
But on my ipad mini (6.0) the mail popped up correctly but when clicked the "send mail" or "cancel" the view was removed and leaving a black screen (everything on the screen is gone)
Any advice will be appreciated, thanks :)
try this code
if ([MFMailComposeViewController canSendMail]) {
mailComposer = [[MFMailComposeViewController alloc]init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:#[#"yourmail#com"]];
[mailComposer setSubject:#"Subject"];
[mailComposer setMessageBody:#"hello \n" isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
}
else{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Error"
message:#"can not send mail with this device"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
}
pragma mark MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (result) {
NSLog(#"Result : %d",result);
}
if (error) {
NSLog(#"Error : %#",error);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I'm using this code for feedback via email in my cocos2d-x game.
Application::getInstance()->openURL("mailto:" + SUPPORT_EMAIL);
You can add subject:
Application::getInstance()->openURL("mailto:" + SUPPORT_EMAIL + "?subject=Hello from NX");
This solution tested on iOS and Android.

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