Send Contact Form Fields in Email - ios

I have a contact form made of text fields (5 fields) that I would like to send via email to a single email address. How do I do this in xCode?

For anyone stumbling across this question, you can use this drop-in iOS contact form.
This fit my needs well, it uses a PHP component to actually send the email. (an example script is included in the sample project.
I posted it to Github here:
https://github.com/mikecheckDev/MDContactForm

The linked post has a similar answer, but I'm adding my code since it checks for canSendMail already. I also left in a bunch of commented code that makes it easy to add other stuff to the email.
Note that this is substantially easier if you are only targeting iOS 5.
I have a free app, QCount, that uses this code. Indeed, I hope I stripped everything custom from my copy-and-paste :-) http://itunes.apple.com/ng/app/qcount/id480084223?mt=8
Enjoy,
Damien
In your .h:
#import <MessageUI/MessageUI.h>
Methods in your .m:
- (void)emailLabelPressed { // or whatever invokes your email
// Create a mail message in the user's preferred mail client
// by opening a mailto URL. The extended mailto URL format
// is documented by RFC 2368 and is supported by Mail.app
// and other modern mail clients.
//
// This routine's prototype makes it easy to connect it as
// the action of a user interface object in Interface Builder.
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
-(void)displayComposerSheet {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Your Form Subject"];
// Take screenshot and attach (optional, obv.)
UIImage *aScreenshot = [self screenshot];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(aScreenshot)];
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"screenshot"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:#"first#example.com", nil];
// NSArray *ccRecipients = [[NSArray alloc] init];
// NSArray *bccRecipients = [[NSArray alloc] init];
// NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
// NSArray *bccRecipients = [NSArray arrayWithObjects:#"fourth#example.com", nil];
[picker setToRecipients:toRecipients];
// [picker setCcRecipients:ccRecipients];
// [picker setBccRecipients:bccRecipients];
// Attach an image to the email.
/* NSString *path = [[NSBundle mainBundle] pathForResource:#"ipodnano"
ofType:#"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"image/png"
fileName:#"ipodnano"];
*/
// Fill out the email body text.
// NSString *emailBody = #"Use this for fixed content.";
NSMutableString *emailBody = [[NSMutableString alloc] init];
[emailBody setString: #"Feedback"];
// programmatically add your 5 fields of content here.
[picker setMessageBody:emailBody isHTML:NO];
// Present the mail composition interface.
if ([self respondsToSelector:#selector(presentViewController:animated:completion:)]) {
[self presentViewController:picker animated:YES completion:nil];
} else {
[self presentModalViewController:picker animated:YES];
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error {
if ([self respondsToSelector:#selector(dismissViewControllerAnimated:completion:)]) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self dismissModalViewControllerAnimated:YES];
}
}
-(void)launchMailAppOnDevice {
NSString *recipients = #"mailto:first#example.com?cc=second#example.com,third#example.com&subject=Hello from California!";
NSString *body = #"&body=Feedback";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

Related

Keyword Search in iOS

I am new to iOS. I have a UITextfield and a Keyword Search Button. When ever I want to search a keyword from a service and press enter. Tt should display the related searched keyword from a service. Please help me to fix this issue? TIA!
- (IBAction)KeywordSearchClicked:(id)sender {
NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
[self KeywordcallSignupProfileService:dict];
}
-(void)KeywordcallSignupProfileService:(NSMutableDictionary *)dict
{
[SVProgressHUD showWithStatus:#"" maskType:SVProgressHUDMaskTypeBlack]; // Progress
NSString * post = [[NSString alloc]initWithFormat:#"userId=%#&key_word%#",UserId,[dict objectForKey:#"key_word"]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.amarbiyashaadi.com/service/amarbiya-service.svc/userKeywordSearch/"]];
RBConnect = [[RBConnection alloc]init];
RBConnect.delegate = self;
[RBConnect postRequestForUrl:url postBody:post];
}
#pragma mark - MRConnection Delegate Methods
- (void)jsonData:(NSDictionary *)jsonDict
{
[SVProgressHUD dismiss];
NSMutableArray *jsonArr;
NSMutableDictionary *userDict,*dict;
NSArray *arr=[jsonDict allKeys];
jsonArr=[jsonDict objectForKey:#"DataTable"];
if (jsonArr.count>0) {
// Save credentials in user defaults
matchesProfileArr=[jsonArr mutableCopy];
DisplayTableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"DisplayTableViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
else
{
NSString *error=#"Somthing Went Wrong";
[SVProgressHUD showErrorWithStatus:error];
}
}

Send email with material from tableview or NSmutableDictionary

In my app, I have a tableView whose material is from my NSmutableDictionary. When I push a button, I want to send these material to a email.Here is the code for sending email:
//Below to send email
- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = #"Course Planner of iBcChem";
// Email Content
NSString *messageBody = #"Please check my course plan";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"xxxxx#gmail.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];//want to fix here
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
Here is my question:
How can I add my material to the messageBody please? Can I use the method similar to show my dictionary like below in my messageBody please?
NSLog(#"Dictionary: %#", [_sectionContents description]);//test dictionary here
Where _sectionContents is my dictionary.
You can attach you dictionary by the same code which you have written.
Here is the code for you.
NSString *emailTitle = #"Course Planner of iBcChem";
NSString *messageBody = #"Please check my course plan";
NSDictionary *dci = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"1",#"2", nil] forKeys:[NSArray arrayWithObjects:#"A",#"B", nil]];
NSArray *toRecipents = [NSArray arrayWithObject:#"xxxxx#gmail.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:dci.description isHTML:NO];
[mc setToRecipients:toRecipents];
[self presentViewController:mc animated:YES completion:NULL];
In simulator mail delegate will take some time to load the Body part of mailcomposersheet. The body will look like.
{
A = 1;
B = 2;
}
Enjoy Coding !!

MFMailComposeViewController dismissed very slow after clicked send button

I have a bit of a strange problem. I am trying to send in-app email. So, I use MFMailComposeViewController for that.And I have realized MFMailComposeViewControllerDelegate.
When I click the send button, it should be dismissed immediately.In debug mode,it works well,BUT,in release mode,it is extremely slow so that user may click send button more than one time and the mail will be sent more than one time.
I have been confused for five days.
Anyone can help me ?
My code:
-(void)sendEMail
{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
if ([mailClass canSendMail])
{
[self displayComposerSheet];
//[self launchMailAppOnDevice];
} else{
[self launchMailAppOnDevice];
}
} else {
[self launchMailAppOnDevice];
}
}
//可以发送邮件的话
-(void)displayComposerSheet
{
//mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
//设置主题
[mailPicker setSubject: kEmailSubject];
// 添加发送者
NSArray *toRecipients = [NSArray arrayWithObject: kToRecipient];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
//NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com", nil];
[mailPicker setToRecipients: toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
// 添加图片
// UIImage *addPic = [UIImage imageNamed: #"123.jpg"];
// NSData *imageData = UIImagePNGRepresentation(addPic); // png
// NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg
// [mailPicker addAttachmentData: imageData mimeType: #"" fileName: #"123.jpg"];
NSString *emailBody = kEmailBody;
[mailPicker setMessageBody:emailBody isHTML:YES];
[self presentViewController:mailPicker animated:YES completion:nil];
}
-(void)sendMailBtnCLicked{
[mailPicker dismissViewControllerAnimated:YES completion:nil];
}
-(void)launchMailAppOnDevice
{
NSString *recipients = kToRecipient;
NSString *subject = kEmailSubject;
//#"mailto:first#example.com?cc=second#example.com,third#example.com&subject=my email!";
NSString *body = kEmailBody;
NSString *email = [NSString stringWithFormat:#"mailto:%#?subject=%#&body=%#", recipients, subject,body];
email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
if (result == MFMailComposeResultSent) {
[AccountManager sharedManager].isSentMail = YES;
}
[controller dismissViewControllerAnimated:YES completion:nil];
//[self dismissModalViewControllerAnimated:YES];
}
Stop other threads running background.
After the mail sent, restart them.

How to use MFMailComposerViewController on iOS 7?

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.

iOS: How can I open my own file format(like .own) in code

I write a file named "test.own" to Document path, and I get its URL.
Now I have a button and what I want is to open an options sheet dialog in which there is Email or others to send or open my file when I click the button.
Is there anyway to achieve this ?
Thanks in advance!
When the file is selected do this.
- (IBAction)showFileOptions:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select a option"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"email file",#"open file"];
[actionSheet showInView:self.view];
}
Write delegate to handle the actionSheet:
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex
{
if (buttonIndex == 0) {
//email
[self emailDocument];
}
else if (buttonIndex==1)
{
//open file
}
}
Emailing document:
-(void)emailDocument
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Your own subject"];
// 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];
// Attach your .own file to the email
//add conversion code here and set mime type properly
NSData *myData =[NSData dataWithContentsOfURL:[NSURL urlWithString:pathToOwnFile]];
[picker addAttachmentData:myData mimeType:#"SETMIMETYPEACCORDINGLY" fileName:#"example.own"];
// Fill out the email body text
NSString *emailBody = #"PFA";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
For e-mail, all you need to do is present the MFMailComposeViewController view and then you can add your ".own" custom document via that view controller's addAttachmentData:mimeType:fileName: method.
(I would link to Apple's documentation except Apple's documentation website appears to be down as I am typing this).
As for the other part of your question, other apps normally use the UIDocumentInteractionController to display a "Open in..." dialog, except the other apps need to know how to open your custom document (which they won't be able to do if your app isn't too big or famous or if somebody else -- who is not -- you authored it).

Resources