This issue is something which has me stumped, so hopefully someone can help. I have an UIActionSheet on a view which has three options in. One which takes my user to a new view, one to share via email and one to share via SMS.
I have the UIActionSheet created which works without issue, the new view part of the AlertSheet also works. I have imported the Message.UI framework and set up the mail and SMS pickers and composers which are fine. However, I am having trouble setting the two 'buttons' on the UIActionSheet to open up the mail and SMS.
Normally i would do this through interface builder and connect a UIButton to the actions I have created, but because this is a UIActionSheet it can't be done that way. Sorry for the LONG code but I felt I needed to show everything, so please see below;
-(IBAction)showActionSheet {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose an Option" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Application Support",#"Share Via Email",#"Share Via SMS",nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
AppSupportView *controller = [[AppSupportView alloc] initWithNibName:#"AppSupportView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if(buttonIndex == 1) {
}
if(buttonIndex == 2) {
}
}
- (void)dealloc {
[feedbackMsg release];
[super dealloc];
}
- (void)viewDidUnload {
self.feedbackMsg = nil;
}
-(IBAction)showMailPicker:(id)sender {
// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later.
// So, we must verify the existence of the above class and provide a workaround for devices running
// earlier versions of the iPhone OS.
// We display an email composition interface if MFMailComposeViewController exists and the device
// can send emails. Display feedback message, otherwise.
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil) {
//[self displayMailComposerSheet];
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail]) {
[self displayMailComposerSheet];
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send mail.";
}
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send mail.";
}
}
-(IBAction)showSMSPicker:(id)sender {
// The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later.
// So, we must verify the existence of the above class and log an error message for devices
// running earlier versions of the iPhone OS. Set feedbackMsg if device doesn't support
// MFMessageComposeViewController API.
Class messageClass = (NSClassFromString(#"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send SMS.";
}
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send SMS.";
}
}
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"My BMR Index Rating from Total:Health App"];
// Set up recipients
//NSArray *toRecipients = [NSArray arrayWithObject:#""];
//[picker setToRecipients:toRecipients];
NSString *emailSharing = #"I Just discovered that I have a Target Heart Rate of";
// Fill out the email body text
[picker setMessageBody:emailSharing isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Displays an SMS composition interface inside the application.
-(void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
NSString *SMSShare = #"I Just discovered that I have a Target Heart Rate of";
// Fill out the email body text
picker.body = SMSShare;
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the
// message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
feedbackMsg.text = #"Result: Mail sending canceled";
break;
case MFMailComposeResultSaved:
feedbackMsg.text = #"Result: Mail saved";
break;
case MFMailComposeResultSent:
feedbackMsg.text = #"Result: Mail sent";
break;
case MFMailComposeResultFailed:
feedbackMsg.text = #"Result: Mail sending failed";
break;
default:
feedbackMsg.text = #"Result: Mail not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
// Dismisses the message composition interface when users tap Cancel or Send. Proceeds to update the
// feedback message field with the result of the operation.
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
feedbackMsg.text = #"Result: SMS sending canceled";
break;
case MessageComposeResultSent:
feedbackMsg.text = #"Result: SMS sent";
break;
case MessageComposeResultFailed:
feedbackMsg.text = #"Result: SMS sending failed";
break;
default:
feedbackMsg.text = #"Result: SMS not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
#end
The issue is obviously that I dont know how to proceed with the (if buttonIndex ==1) etc piece of code to open the mail and SMS
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
AppSupportView *controller = [[AppSupportView alloc] initWithNibName:#"AppSupportView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if(buttonIndex == 1) {
}
if(buttonIndex == 2) {
}
}
Any help would be appreciated.
Thanks
looks like all your needed methods are there already..
just add [self showMailPicker:nil] or [self showSMSPicker:nil] to
if(buttonIndex == 1) {
}
if(buttonIndex == 2) {
}
if your second button from the top is your sms button, add showSMSPicker to buttonIndex == 1
Related
When I try to show the facebook share after having sent an email (using MFMailComposeViewController) I get this error:
A view can only be associated with at most one view controller at a
time! View [EAGLView] is associated with [EmailViewController]. Clear
this association before associating this view with [FacebookView].'
[EmailViewController removeFromParentViewController]; Does nothing
EmailViewController.view = nil; Causes a white screen, even though the email form is long gone.
How to make it forget that I ever sent and email and make the view hierarchy go back to how it was before? The facebook share works if I haven't sent an email.
-(IBAction)ShowEmailForm:(char*)pSubject :(char*)pBody :(char*)pTo
{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
if ([mailClass canSendMail])
{
self.view = eaglView;
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:[NSString stringWithFormat:#"%s", pSubject]];
// Set up recipients
if( pTo != nil )
{
NSArray *toRecipients = [NSArray arrayWithObject:[NSString stringWithFormat:#"%s", pTo]];
[picker setToRecipients:toRecipients];
}
// Fill out the email body text
[picker setMessageBody:[NSString stringWithFormat:#"%s",pBody] isHTML:YES];
[self presentViewController:picker animated:YES completion:^(){}];
[picker release];
}
}
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissViewControllerAnimated:YES completion:^(){ printf("Email form done dismissing.\n"); }];
printf("Email form dismissed.\n");
[self removeFromParentViewController];
//Email was sent.
if (result == MFMailComposeResultSent)
{
printf("Email Sent!\n");
NSString *pEmail = [self findEmailAddresses:controller.view : 0];
}
}
Fixed it.
Change
self.view = eaglView;
to
[eaglView addSubview:self.view];
- (IBAction)SendTxt:(id)sender {
MFMessageComposeViewController *textComposer = [[MFMessageComposeViewController alloc] init];
[textComposer setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
[textComposer setRecipients:[NSArray arrayWithObjects:#"123456",#"123456", nil]];
[textComposer setBody:#"HELP ME"];
[self presentViewController:textComposer animated:YES completion:NO];
}
else {
NSLog(#"Can't open Text");
}
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultSent:
NSLog(#"SENT");
break;
case MessageComposeResultFailed:
NSLog(#"FAILED");
break;
case MessageComposeResultCancelled:
NSLog(#"CANCELLED");
break;
default:
break;
}
}
I have the above code which brings up the sms window as normal..
It works ok and sends a SMS but the SMS window will not close after sending or cancelling..
The NSLog registers the send or cancel but the SMS window will not go away!
Can anyone help?
Thanks
Mat
You have to dismiss it yourself using
[self dismissViewControllerAnimated:YES completion:^{ // something to do on completion if you need}];
I want to be able to send an email from my app, in which I have got that portion working. The problem is when I cancel sending an email (which is what I'm testing for now) the email part is dismissed but I'm left with a black screen which I can't seem to dismiss.
So this is what I have. I've created a class to handle the email part:
MailViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>
#end
Implemented as follows:
#import "MailViewController.h"
#interface MailViewController ()
#end
#implementation MailViewController
- (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.
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
[mailer setMailComposeDelegate:self];
[mailer setSubject:#"Subject"];
NSMutableArray *toArray = [[NSMutableArray alloc] initWithObjects:#"email#gmail.com", nil];
[mailer setToRecipients:toArray];
[self presentViewController:mailer animated:YES completion:nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failuer" message:#"This device is unable to send emails" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result){
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email was sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Mail sent");
break;
default:
NSLog(#"Mail not sent");
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
#end
And all of this is shown from another view like so:
-(IBAction)sendMessage:(id)sender{
NSLog(#"Going to send an email....");
MailViewController *mail = [[MailViewController alloc] init];
[self.navigationController pushViewController:mail animated:YES];
}
So when I run all of this, the email program opens and I can do email stuff. I can select Cancel, then Delete Draft and the email program is removed. However, I'm left with a black screen which I can then select Back from the top navigation bar to return to the previous view.
I simply want the app to return to the view that shows the email program when an email is sent or cancelled (or whatever). I'm sure it's something simple I'm missing.
The problem is tat you are pushing a MailViewController and then dimissing, if you push you should pop the view controller.
MFMailComposeViewControllers should be presented modally, not pushed on the navigation stack. You also dont need a subclass, you can create an instance of MFMailComposeViewController directly:
-(IBAction)sendMessage:(id)sender{
NSLog(#"Going to send an email....");
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[self presentViewController:mail animated:YES completion:nil];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result){
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email was sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Mail sent");
break;
default:
NSLog(#"Mail not sent");
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
There's a couple architecture questions I have, but first - the TL;DR version:
[self.navigationController popViewControllerAnimated:NO];
You could also choose YES for the boolean, but judging on your implementation, that wouldn't result in something nice. Put that in the delegate call for completion.
However, why not just have whatever's pushing MailViewController onto the stack handle the job of being the MFMailComposeViewControllerDelegate? You're doing nothing here that requires a completely new view controller be pushed onto the stack.
In my iPhone application, I have just implemented in-app SMS functionality. SMS functionality is working fine. But after opening MFMessageComposeViewController, if user wants to cancel sending sms, they have no option. The only option left is to send an sms, then only return on previous view. There should be a cancel button on navigation bar just as it is in the email composer. Below is the line of code that I wrote to have in-app sms functionality:
-(void) smsComposer{
MFMessageComposeViewController *_smsCompose = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText]) {
_smsCompose.body = #"SMS BODY";
_smsCompose.messageComposeDelegate = self;
[self presentModalViewController:_smsCompose animated:YES];
}
}
Is there anything that I am missing?
Thanks in advance,
PC
Try This....
in .h file
#import <MessageUI/MFMessageComposeViewController.h>
and
#interface TestViewController : UIViewController <MFMessageComposeViewControllerDelegate>
And then Button Click method
-(void)buttonPressed:(UIButton *)button
{
[self sendSMS:#"Body of SMS..." recipientList:[NSArray arrayWithObjects:#"+1-111-222-3333", #"111-333-4444", nil]];
}
MFMessageComposeViewController to create the SMS content and another method for handling the user interaction with the SMS dialog.
-(void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
And
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultCancelled)
NSLog(#"Message cancelled")
else if (result == MessageComposeResultSent)
NSLog(#"Message sent")
else
NSLog(#"Message failed")
}
And remember: You cannot send SMS messages from within the simulator. Test on Device.
I am sending an email from my iPhone app using MFMailComposeViewController. This works fine but after sending or canceling I need to dismiss the modalViewController. When I do this I get a Program received signal: “EXC_BAD_ACCESS”. This is not very descriptive... Please help!!
This is the code for creating the mail and the modalViewController
-(void)sendFavMail:(NSString *)body{
MFMailComposeViewController* mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"Favorites List"];
[mailViewController setMessageBody:body isHTML:YES];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
}
And this is the code for the delegate, dismissing the modalviewcontroller:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Cancelled sending");
break;
case MFMailComposeResultSaved:
NSLog(#"Message Saved");
break;
case MFMailComposeResultSent:
NSLog(#"Message Sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Sending Failed");
break;
default:
NSLog(#"Message not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Thanks for your help!!
Darn, fixed it myself :-)
I released an object in the body of the message before sending/cancelling. What I did to fix it is to declare this body object autoreleased. And what do you know? IT WORKS!
Just answered my own question...