Compose mail in the iPhone app - iphone

In my app, I have an abouts page. I would like to place a round rect button which when I press it, I would be able to send an email to the company with an embedded email address.
Are there any tutorials to do this?
Thanks in advance.

Here's the code:
Obj-C:
(Don't forget to add the messageUI framework to your project!!!)
First import the message library:
#import <MessageUI/MessageUI.h>
Then mark your self as a delegate like this:
#interface MYViewController () <MFMailComposeViewControllerDelegate>
Then to pull up the composer (if user has email set up on their device):
- (IBAction)emailButtonPressed:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
[composeViewController setMailComposeDelegate:self];
[composeViewController setToRecipients:#[#"example#email.com"]];
[composeViewController setSubject:#"example subject"];
[self presentViewController:composeViewController animated:YES completion:nil];
}
}
Then to handle the delegate callback and dismiss the composer:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
//Add an alert in case of failure
[self dismissViewControllerAnimated:YES completion:nil];
}
SWIFT 3:
Import the relevant library:
import MessageUI
Mark your view controller as a delegate like so:
class MyViewController: UIViewController, MFMailComposeViewControllerDelegate {
Pull up composer (if user has email set up on their device):
#IBAction func emailButtonAction(_ sender: UIButton) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["example#gmail.com"])
mail.setSubject("Example Subject")
mail.setMessageBody("<p>Test</p>", isHTML: true)
present(mail, animated: true)
}
}
Handle delegate callback and dismiss the composer:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}

You have to link against the MessageUI framework and use the class MFMailComposeViewController. Don't forget to import the framework (#import <MessageUI/MessageUI.h>).
The documentation with sample code: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

Here is the code to open Mail Composer in iOS:
Source:http://sickprogrammersarea.blogspot.in/2014/03/open-mail-composer-programmatically-in.html
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(send:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Send Mail" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 350.0, 100.0, 40.0);
[self.view addSubview:button];
}
- (void) send:(id)sender{
MFMailComposeViewController *comp=[[MFMailComposeViewController alloc]init];
[comp setMailComposeDelegate:self];
if([MFMailComposeViewController canSendMail])
{
[comp setToRecipients:[NSArray arrayWithObjects:#"imstkgp#gnail.com", nil]];
[comp setSubject:#"From my app"];
[comp setMessageBody:#"Hello bro" isHTML:NO];
[comp setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:comp animated:YES completion:nil];
}
else{
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:nil cancelButtonTitle:#"" otherButtonTitles:nil, nil];
[alrt show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if(error)
{
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:nil cancelButtonTitle:#"" otherButtonTitles:nil, nil];
[alrt show];
[self dismissModalViewControllerAnimated:YES];
}
else{
[self dismissModalViewControllerAnimated:YES];
}
}

Lots of them. You can start here and here. And check stackoverflow right here.

Related

clickable text in form of email + custom alert view

I want in my app that email id is written in text form off-course, but that text should be clickable. And on clicking that a pop up should appear having two fields and two buttons. The two fields should be editable so as to type "from" and "message body" into it.The two buttons are of send and cancel.
please help.
just create button with your Email Address and set selector on it like bellow...
UIButton *BtnEmail = [UIButton buttonWithType:UIButtonTypeCustom];
[BtnEmail setBackgroundColor:[UIColor clearColor]];
[BtnEmail setTitle:#"YourEmailId" forState:UIControlStateNormal];
BtnEmail.frame = CGRectMake(85, 100, 150, 39 );/// set your frame
[BtnEmail addTarget:self action:#selector(EmailButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:BtnEmail];
and in action event (Method) write this code.. and just add these two delegate in .h file MFMessageComposeViewControllerDelegate and MFMailComposeViewControllerDelegate and also 1 framework name is MessageUI.framework
-(IBAction)EmailButtonClicked:(id)sender{
if ([MFMailComposeViewController canSendMail]) {
appDelegate.imgCapture = [self captureView];
[appDelegate.imgCapture retain];
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
NSString *mailBody = #"Set Your Body Message";
[mailComposeViewController setMessageBody:mailBody isHTML:NO];
mailComposeViewController.mailComposeDelegate = self;
[self presentViewController:mailComposeViewController animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"e-Mail Sending Alert"
message:#"You can't send a mail"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
and these bellow method is Delegate method..
#pragma mark - MFMessage Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
i hope this help you...

How to send mail in xcode using ARC

Okay after finding a couple of tutorials online. Everything is okay except when the button is being pressed. I pressed the button that will sent an email and suddenly there is a signal abort. I watched two videos and re type every code in each video twice. Why is there a signal abort? Please help. Thank you!
this is the .h file
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface secondViewController : UIViewController <MFMailComposeViewControllerDelegate> {
IBOutlet UILabel *resultLabel;
}
-(IBAction)switchToFirst:(id)sender;
-(IBAction)openMail:(id)sender;
#end
this is the .m file
#import "secondViewController.h"
#import "ViewController.h"
#interface secondViewController ()
#end
#implementation secondViewController
-(IBAction)openMail:(id)sender {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"", nil]];
[composer setSubject:#""];
[composer setMessageBody:#"message" isHTML:YES];
[composer setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:composer animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:#"Can't send your email!" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultSent:
resultLabel.text = #"Mail was sent";
break;
case MFMailComposeResultSaved:
resultLabel.text = #"Mail was saved to drafts";
break;
case MFMailComposeResultFailed:
resultLabel.text = #"Mail wasn't able to sent";
break;
case MFMailComposeResultCancelled:
resultLabel.text = #"Mail was Cancelled";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction)switchToFirst:(id)sender {
ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Set the background image by setting the contents of the view's layer
UIImage *bg = [UIImage imageNamed:#"iPad iDeaf Assisstant Background.png"];
self.view.layer.contents = (id) [bg CGImage];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
If you have a class for email sending you can compile it without arc, just set flag -fno-objc-arc for files in project settings->build phases
Like #qegal said, just take out all the retains and releases.
And if you want to keep the code you have, you can just mark files as non-arc.
To mark any file as non-arc you can set a compiler flag in the build phases. Select your project in the files pane on the left hand side. Then select your target, go to build phases, and double click the column underneath compiler flags for the file you want to mark and enter 'fno-objc-arc'.

How to get the music files from the library

iam developing one application.In that i need to get the music files from the phone library.For that iam using the MPMediaPickerCOntroller.But it doesn't fire the didpickingitem delegate method.My code like below.
- (void)viewDidLoad
{
[super viewDidLoad];
MPMediaPickerController *picker =[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.prompt =#"Add songs to play";
[self presentModalViewController:picker animated: YES];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
NSLog(#"sdfadsf");
NSLog(#"%#",mediaItemCollection);
NSArray *slist=[mediaItemCollection copy];
NSLog(#"%#",slist);
}
Do you declare the MPMediaPickerControllerDelegate in your .h?
ex:
#interface FirstViewController : UIViewController
If you do not that would explain why the method isn't firing. Also, I do not believe it will work on the simulator; only on devices.
EDIT:
-(IBAction)presentLibrary:(id)sender
{
//this is called from a button press, but you could do it in viewDidLoad
MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = NO;
picker.prompt = NSLocalizedString (#"Select any song from the list", #"Prompt to user to choose some songs to play");
[self presentModalViewController: picker animated: YES];
}
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
NSLog(#"didpick");
if (mediaItemCollection) {
[_mediaPlayer setQueueWithItemCollection: mediaItemCollection];
_mediaCollection = mediaItemCollection;
}
[mediaPicker dismissModalViewControllerAnimated:YES];
}
Read from here The Music Player Framework in iPhone

not able to send mail from Iphone application

#interface ViewController : UIViewController { //....yor variables }
in ViewController.m file:
(void)viewDidLoad {
[super viewDidLoad];
self.title = #"Sample Email Application"; // title of navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(composeMail:)] autorelease]; // for adding a compose button //in navigation bar. //...your code }
-(void) composeMail: (id) sender{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate = self;
[[picker navigationBar] setTintColor:[UIColor blackColor]];
[picker setSubject:#"Sample Email Application"];
[picker setMessageBody:[NSString stringWithFormat:#"Visit for more help %#. ",#"http://google.com"] isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[controller dismissModalViewControllerAnimated:YES];
}
It looks like you are missing the -setToRecipients, which you can't send mail unless you have an address to send to.
[picker setToRecipients:[NSArray arrayWithObjects:#"your#emailstring.com", nil]];

Can't dismiss the email composer view in iPhone?

I am new to iphone development.I have created a tabbar based application . In the first i want the email composer to be displayed. I am able to display it but the cancel and send button are not working,I don't know where do i go wrong .Please help me out. Here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
[self displayComposerSheet];
}
-(void)displayComposerSheet
{
picker = [[MFMailComposeViewController alloc] init];
[[picker navigationBar] setTintColor:[UIColor blackColor]];
picker.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail])
{
[picker setToRecipients:[NSArray arrayWithObjects:#"name#gmail.com",nil]];
[picker setSubject:#"Sample"];
}
[self.view addSubview:picker.view];
[self presentModalViewController:picker animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
}
You are presenting the mail composer twice.
Remove the line:
[self.view addSubview:picker.view];
And replace the next line with:
[self.navigationController presentModalViewController:picker animated:YES];
If you are adding only subview of mailcomposser you have to remove it from self.view,
In your code you are adding subview and present also,
If you are use only use [self.view addSubview:picker.view]; than
Try with to remove it.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[controller.view removeFromSuperview];
}
I'm still suggest to use
[self.navigationController presentModalViewController:picker animated:YES]; for Present MFMailComposeViewController ,
and use [self dismissModalViewControllerAnimated:YES]; to dismiss it.
Use this code:
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:#"niftyapplications#gmail.com", #"support#niftysol.com", nil];
[controller setToRecipients:toRecipients];
[controller setTitle:#"Contact Us"];
controller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:controller animated:YES];
- (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(#"ISO Audit",#"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alertView show];
[self dismissModalViewControllerAnimated:YES];
}
Set Delegate of MFMailComposeViewController
MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc]init];
mailcomposer.mailComposeDelegate = self;
And Use this Delegate Method
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}