#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]];
Related
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.
I have a big problem since a few days that I can't solve.
First I have a login view controller with this code :
#implementation MMConnectionViewController
#synthesize login, password, activityIndicator, mainVC;
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
[self performSelectorOnMainThread:#selector(startRolling) withObject:nil waitUntilDone:NO];
[NSThread detachNewThreadSelector:#selector(connect) toTarget:self withObject:nil];
return YES;
}
- (void)viewWillAppear:(BOOL)flag {
[super viewWillAppear:flag];
[login becomeFirstResponder];
login.keyboardAppearance = UIKeyboardAppearanceAlert;
password.keyboardAppearance = UIKeyboardAppearanceAlert;
[self setTitle:#"Monaco Marine"];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Logout"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
[backBarButtonItem release];
}
- (void)connect {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
mainVC = [[MMMainViewController alloc] initWithLogin:login.text password:password.text connectPass:#"1" navigationController:self.navigationController nibName:#"MMMainViewController" bundle:nil];
if (mainVC) {
[self performSelectorOnMainThread:#selector(dataLoadingFinished) withObject:nil waitUntilDone:YES];
}
[pool release];
}
- (void)dataLoadingFinished {
self.stopRolling;
[self.navigationController pushViewController:mainVC animated:YES];
}
- (void)showAlertWithMessage:(NSString *)message {
self.stopRolling;
NSLog(#"%#",message);
UIAlertView *warning = [[UIAlertView alloc] initWithTitle:#"Connection Failed" message:[NSString stringWithFormat:#"%#",message] delegate:self cancelButtonTitle:#"Retry" otherButtonTitles:nil];
[warning show];
[warning release];
}
- (void)startRolling {
[activityIndicator startAnimating];
}
- (void)stopRolling {
[activityIndicator stopAnimating];
}
- (void)viewDidLoad {
[login becomeFirstResponder];
}
- (void)dealloc {
[login release],login=nil;
[password release],password=nil;
[activityIndicator release],activityIndicator=nil;
[super dealloc];
}
After, there's MMMainViewController with this code :
#implementation MMMainViewController
#synthesize login, password, connectPass, navigationController, accountVC;
- (void)viewDidLoad {
// Set a title for each view controller. These will also be names of each tab
accountVC.title = #"Account";
accountVC.tabBarItem.image = [UIImage imageNamed:#"icon_user.png"];
self.view.frame = CGRectMake(0, 0, 320, 480);
// Set each tab to show an appropriate view controller
[self setViewControllers:
[NSArray arrayWithObjects:accountVC, nil]];
[navigationController setNavigationBarHidden:NO animated:NO];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Menu"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self setTitle:#"Menu"];
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithLogin:(NSString *)l password:(NSString *)p connectPass:(NSString *)c navigationController:(UINavigationController *)navController nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
login = l;
password = p;
connectPass = c;
navigationController = navController;
if (!accountVC)
accountVC = [MMAccountViewController alloc];
[self.accountVC
initWithNibName:#"MMAccountViewController" bundle:nil];
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
- (void)dealloc {
[connectPass release];
[login release];
[password release];
[super dealloc];
}
The layer MMAccountViewController loaded from MMMainViewController is a basic view controller with nothing in it.
Now the problem is that sometimes when load my tabbed view controller and I go back to the login view controller, the screen freezes and bug with message (NSZombieEnabled = YES) :
*** -[CALayer retain]: message sent to deallocated instance 0xd0199d0
This is all I have and I really can't see where I'm wrong.
Some more idea?
Thanks for those who help me!
You are overreleasing something somewhere. You might want to run your application in Instruments to check where it may be happening (XCode: Run->Run With Performance Tool->Leaks will give you the setup that you need afaik). I can't see anything in your code, but you said yourself that you use "roughly" this code, so it may not be in this part of your program.
Update:
I still can't see that you overrelease something somewhere... I'm pretty sure the problem is not within this part of the code. If you haven't found the problem yet, you may want to try and create a test case, that is, a small program that tries to mimic this programs behavior and reproduce the bug. If you can reproduce it within a small program, I'll take a look at that.
I was also encounter the same problem, and the problem was that I was assigning a UIButton to the rightNavigationItem and release that button instance, I just remove the release code and started working.
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
{
}
I have a hierarchy of views:
compView with two children:
bgView
objectView.
I'm passing objectView to the cameraOverlayView property of the UIImagePickerController. Once the ImagePicker is done I update the image in the bgView and I would like to continue to display the view tree including the objectView which was passed to the UIImagePickerController as an overlayView.
The problem is that after calling dismissModalViewControllerAnimated to close the UIImagePickerController, the objectView is not appearing, as if the UIImagePickerContoller is hiding it. I checked in the debugger and it doesn't seem to be dealocated. The pointer is still valid. But the view is not showing.
Any ideas of what I might be doing wrong?
Thanks,
Eddy
Here a bit of code to show you what I'm doing:
From myViewController.m:
- (void)loadView {
CGRect rect = self.navigationController.view.bounds;
compView = [[UIView alloc] initWithFrame:rect];
bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Storage/dummy_bg.png"]];
objectView = [[PropImageView alloc] initWithImage:[UIImage imageNamed:#"Storage/rasta.png"]];
[objectView setUserInteractionEnabled:YES];
[objectView setMultipleTouchEnabled:YES];
[compView addSubview:bgView];
[compView addSubview:objectView];
self.view = compView;
[self placeObject];
[objectView release];
[bgView release];
[compView release];
}
- (void) placeObject
{
NSLog(#"camera button pressed");
// make sure camera is avaialble before setting it as source
//
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
#if !TARGET_IPHONE_SIMULATOR
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing = NO;
picker.view.hidden = NO;
picker.cameraOverlayView = objectView;
[self presentModalViewController:picker animated:NO];
[picker release];
#endif
}
// from PickImageAppDelegate.m
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
// Dismiss the image selection, hide the picker and
[picker dismissModalViewControllerAnimated:YES];
bgView.image = [self rotateImage:image byOrientationFlag:UIImageOrientationRight];
}
[picker dismissModalViewControllerAnimated:YES];
That should be
[self dismissModalViewControllerAnimated:YES];
Apple provides the code to use MFMailComposeViewController.but it uses
- (IBAction)buttonPressed
{
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc]init];
controller.mailComposeDelegate = self;
[controller setSubject:#"In app email..."];
[controller setMessageBody:#"...a tutorial from mobileorchard.com" isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
by default it uses bottom UP transition.suppose if i want to use following, it gives wrong
ouput.can i use other add subview like that instead of presentModalViewController
{
UIViewAnimationTransition trans = UIViewAnimationTransitionFlipFromRight;
[UIView beginAnimations: nil context: nil];
[UIView setAnimationTransition:trans forView: [self view] cache: YES];
[self presentModalViewController: controller animated:YES];
[UIView commitAnimations];
}
it works correctly for other view controller,but it did not work in MFMailComposeViewController any help please?
hi i have done like this,but current view controller flips, and then composer comes from
bottom..?will you help?
- (IBAction)clickedMailButton:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mcontroller = [[MFMailComposeViewController alloc]init];
//[mcontroller setSubject:#"My Pocket Schedule"];
[mcontroller setTitle:#"New Message"];
[mcontroller setMessageBody:#"Check out My Pocket Schedule in the iTune Store" isHTML:NO];
mcontroller.mailComposeDelegate = self;
UIViewAnimationTransition trans = UIViewAnimationTransitionFlipFromRight;
[UIView beginAnimations: nil context: nil];
[UIView setAnimationTransition:trans forView: [self view] cache: YES];
[self presentModalViewController:mcontroller animated:YES];
[UIView commitAnimations];
[mcontroller release];
}
Use the modalTransitionStyle property:
MFMailComposeViewController *mcontroller = [[MFMailComposeViewController alloc] init];
[mcontroller setTitle:#"..."];
[mcontroller setMessageBody:#"..." isHTML:NO];
mcontroller.mailComposeDelegate = self;
mcontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:mcontroller animated:YES];
[mcontroller release];
Try
presentModalViewController:withTransition: