I have a webView in my app which loads from local HTML file.
HTML file contains a Mail link.
if I click on mail then it opens Default Mail Application to send mail.
But I need to open a mailComposer within my app.
my code is as follow
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"%#",request.URL);
if ([[[request URL] scheme] isEqualToString:#"mailto"]) {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
NSString *strEmail = [NSString stringWithFormat:#"%#",request.URL];
NSString *subString = [[strEmail componentsSeparatedByString:#":"] lastObject];
[composer setToRecipients:[NSArray arrayWithObjects:subString, nil]];
[composer setSubject:#"Kreativ-Q"];
[composer setMessageBody:#"" isHTML:YES];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:composer animated:YES completion:nil];
} }
return YES;
}
help me to solve this
Thanks
Related
I just need to display the email composer on my iOS simulator (No need to send the actual mail).
As soon as I am initializing the MFMailComposeViewController I am getting alert pop - up saying that email account is not set.
This is what I am doing.
MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init];
This line pops up the alert controller.
For simulator email configuration is not available.
Use the below code to send email.
- (void)openEmail
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mail =
[[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Subject"];
[mail setMessageBody:#"body message" isHTML:NO];
[mail setToRecipients:#[#"email1#example.com",#"email2#example.com"]];
/// if attachment then implement below line
//[mail addAttachmentData:<#(nonnull NSData *)#> mimeType:<#(nonnull NSString *)#> fileName:<#(nonnull NSString *)#>]
[self presentViewController:mail animated:YES completion:NULL];
}
else {
NSLog(#"Please configure your email. Settings->Accounts & Passwords->Add Account");
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error
{
switch (result) {
case MFMailComposeResultSent:
NSLog(#"Sent");
break;
default:
break;
}
}
So basically I want to share the content only through mail and I do not want to show the option of message. Could you help me out with that. Also I want to set the subject of the email through the code and also the recipient of the email through the code
You can put this in the method for a button tap or something like that.
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = controller;
[mailViewController setSubject:subject];
NSMutableArray *emails = [[NSMutableArray alloc] init];
[emails addObject:address];
[mailViewController setToRecipients:emails];
mailViewController.navigationBar.translucent = NO;
mailViewController.navigationBar.tintColor = [UIColor whiteColor];
mailViewController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
[controller presentViewController:mailViewController animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}];
}
My MFMailComposer code:
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
[mailViewController setDelegate:self];
[mailViewController setSubject:#"subject"];
[mailViewController setToRecipients:[NSArray arrayWithObject:#"email#email.com"]];
[mailViewController setMessageBody:#"body" isHTML:NO];
for (int i = 0; i < self.imagesData.count; i++)
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd-HH-mm-ss-SSSS"];
[mailViewController addAttachmentData:[self.imagesData objectAtIndex:i] mimeType:[self contentTypeForImageData:[self.imagesData objectAtIndex:i]] fileName:[NSString stringWithFormat:#"portfolio_%#_%d", [dateFormatter stringFromDate:[NSDate date]], i]];
}
[self presentViewController:mailViewController animated:YES completion:nil];
With this code I'd press cancel and delete draft and nothing happens, the MFMailComposerViewController doesn't close.
I added the delegates already:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
if(error) NSLog(#"ERROR - mailComposeController: %#", [error localizedDescription]);
if (result == MFMailComposeResultSent)
{
NSLog(#"It's away!");
}
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[self dismissViewControllerAnimated:YES completion:nil];}
What am I missing?
OK I found an answer. I was stupid.
I used setDelegate instead of setMailComposeDelegate
I'm trying to send mail to list of email array that I receive from database, when I send the recipient list gets populated in iOS 7 but when I tried in iOS 5 the recipient list doesn't get populated. Any Idea why? This is my mail function
-(void)sendEmailToContacts:(NSArray *)fList withText:(NSString *)emailText withTag:(NSInteger )tag
{
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.view.tag=tag;
NSString *htmlBody =[NSString stringWithFormat:#"%#",_currentAdd.contentUrl,addtext];
[mailComposer setMessageBody:htmlBody isHTML:YES];
[mailComposer setSubject:_currentMail.subject];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:fList];
[self presentViewController:mailComposer animated:YES completion:nil];
}
else
{
NSLog(#"Device is unable to send email in its current state.");
}
}
My fList (recipient list) is an NSArray, this is a sample output of my fList
(
"john#gmail.com",
"mary#gmail.com",
"akhil#gmail.com",
"tester#gmail.com"
)
Recipients are expected as immutable array. check your array type
NSArray *usersTo = [NSArray arrayWithObject: #"raja#apple.com"];
[mailComposer setToRecipients:usersTo];
Try this one.
NSArray *fList = [NSArray arrayWithObjects:#"raja#apple.com",#"john#gmail.com",#"mary#gmail.com",#"akhil#gmail.com",#"tester#gmail.com", nil];
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.view.tag=tag;
NSString *htmlBody =[NSString stringWithFormat:#"%#",_currentAdd.contentUrl,addtext];
[mailComposer setMessageBody:htmlBody isHTML:YES];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:_currentMail.subject];
mailComposer.delegate = self;
[mailComposer setToRecipients:fList];
[self presentViewController:mailComposer animated:YES completion:nil];
-(void)sendEmailToContacts:(NSArray *)fList withText:(NSString *)emailText withTag:(NSInteger )tag
{
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
//mailComposer.view.tag=tag;
NSString *htmlBody =[NSString stringWithFormat:#"%#",_currentAdd.contentUrl,addtext];
[mailComposer setMessageBody:htmlBody isHTML:YES];
[mailComposer setSubject:_currentMail.subject];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:fList];
[self presentViewController:mailComposer animated:YES completion:nil];
}
else
{
NSLog(#"Device is unable to send email in its current state.");
}
}
Apparently the issue was with setting tag if I try to set the tag before setToRecipients line it will not show the recipients list in iOS 5, it will work if the setting tag line is commented out or set after setToRecipients.
I am developing an app for iOS 7 and used MFMailComposerViewController.
I have tried everything but dismissViewController:withAnimated is not working.
sometimes class automatically call delegate by itself when it first displays viewController using method presentViewCOntroller:withAnimated:completion.
My app is navigation based that's why I think issue is just related with UINavigationController as well.
-(void)sendMail{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from California!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
NSMutableString *emailBody =[NSMutableString stringWithString: #"<table border=1 align=\"center\"><tr><th>EventDate</th><th>EventDay</th><th>EventTime</th><th>Speaker</th><th>topic</th></tr>"];
for (int i=0; i<5; i++) {
NSString *eventDate=[NSString stringWithFormat:#"<tr><td>%#</td>",#"12/11"];
NSString *eventDay=[NSString stringWithFormat:#"<td>%#</td>",#"Sunday"];
NSString *eventTime=[NSString stringWithFormat:#"<td>%#</td>",#"12:10 pm"];
NSString *eventSpeaker=[NSString stringWithFormat:#"<td>%#</td>",#"RajVeer"];
NSString *eventTopic=[NSString stringWithFormat:#"<td>%#</td>",#"nano-technology"];
NSString *dataString=[NSString stringWithFormat:#"%#%#%#%#%#</tr>",eventDate,eventDay,eventTime,eventSpeaker,eventTopic];
[emailBody appendString:dataString];
}
NSString *lastTable=#"</table>";
[emailBody appendString:lastTable];
NSLog(#"%#",emailBody);
[picker setMessageBody:emailBody isHTML:YES];
[self presentViewController:picker animated:YES completion:NULL];
}
This should do the trick:
#pragma mark MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[controller dismissViewControllerAnimated:YES completion:nil];
}
Use this code to present MFMailComposeViewController
[self presentViewController:mailComposerObject animated:YES completion:NULL];
For dismiss MFMailComposeViewController
#pragma mark - MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:NULL];
}
from iOS 6.0 [self presentModalViewController:<#(UIViewController *)#> animated:<#(BOOL)#>] is deprecated.