Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 496357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:40:43+00:00 2026-05-13T05:40:43+00:00

This problem is probably not specific to MFMailComposeViewController, but that is where I am

  • 0

This problem is probably not specific to MFMailComposeViewController, but that is where I am having the problem…

  • I am building the NSString
    “myEmailString” for the messageBody of
    the MFMailComposeViewController and
    storing it in an iVar before
    displaying the
    MFMailComposeViewController as a
    modal view controller.

  • I pass the string into the MFMailComposeViewController, then present it as a modal view controller.

  • When the modal view controller is dismissed, my iVar becomes invalid,
    and the app crashes when I release the emailString iVar in dealloc

Code below, what am I doing wrong?

-(void)buildEmailMessage {
int mySection;
int myRow;
NSString *buildString = [NSString stringWithFormat:@"<b><p>Ten Essentials Check List</b><br />%@</p>", [myList valueForKey:@"listName"]];

for (mySection = 0; mySection < [[fetchedResultsController sections] count]; mySection ++) {
    NSString *sectionName = [NSString stringWithFormat:@"<p><b>%@ Group</b></p><ul>", [[[fetchedResultsController sections] objectAtIndex:mySection] name]];
    buildString = [buildString stringByAppendingString:sectionName];
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:mySection];
    for (myRow = 0; myRow < [sectionInfo numberOfObjects]; myRow ++) {

        // Get the managedObject
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:myRow inSection:mySection];

        NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
        //Get the related Item object
        Item *item  = [managedObject valueForKey:@"item"];
        NSString *itemName = [NSString stringWithFormat:@"<li>%@</li>", item.itemName];
        buildString = [buildString stringByAppendingString:itemName];

    }
    buildString = [buildString stringByAppendingString:@"</ul>"];
}


myEmailString = [NSString stringWithString:buildString];
NSLog(@"email string = :\n%@", myEmailString);
[self showPicker];
}



#pragma mark -
#pragma mark Send Mail

-(void)showPicker {
// This code can run on devices running iPhone OS 2.0 or later  
// 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.
// We launch the Mail application on the device, otherwise.

NSLog(@"Checking OS for MFMailComposeViewController");

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];
}
}




// Displays an email composition interface inside the application. Populates all the Mail fields. 
-(void)displayComposerSheet {

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
picker.navigationBar.barStyle = UIBarStyleBlack;


[picker setSubject:@"Here is your gear check list!"];


// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"Checkmark_icon" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"Checkmark_icon"];

// Fill out the email body text
//***** NOTE: This is where I pass the value from my iVar ***** 
//      into the MFMailComposeViewController
// 
NSString *emailBody = [NSString stringWithString:myEmailString];
[picker setMessageBody:emailBody isHTML:YES];
NSLog (@"DIsplaying Composer Sheet");

[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  { 
//message.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog (@"Result: canceled");
        break;
    case MFMailComposeResultSaved:
        NSLog (@"Result: saved");
        break;
    case MFMailComposeResultSent:
        NSLog (@"Result: sent");
        break;
    case MFMailComposeResultFailed:
        NSLog (@"Result: failed");
        break;
    default:
        NSLog (@"Result: not sent");
        break;
}
[self dismissModalViewControllerAnimated:YES];

// ***** NOTE: Line below was added to fix the invalid iVar problem *****
myEmailString = @"";
}


#pragma mark -
#pragma mark Workaround

// Launches the Mail application on the device.
-(void)launchMailAppOnDevice {
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Here is your gear check list!";
NSString *body = myEmailString;

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

- (void)dealloc {
[managedObjectContext release];
[fetchedResultsController release];
[tableview release];
[myList release];
[myEmailString release];

[super dealloc];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:40:43+00:00Added an answer on May 13, 2026 at 5:40 am

    how is your ivar declared? is it declared as a property? in any case, it is not automatically retained for you.

    Either you need to do

    myEmailString = [[NSString stringWithString:buildString] retain];
    

    or

    self.myEmailString = [NSString stringWithString:buildString];
    

    if you have myEmailString declared as

    @property (nonatomic, retain) NSString *myEmailString
    

    Think about it: if all ivars were automatically retained for you, then how would you have a variable that you didn’t want to retain? That’s why it doesn’t work that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably really stupid but i can't find the problem with my code.
This is probably got a simple answer to it, but I am having problems
This problem is probably very easy to solve but somehow I cannot find a
This is probably the most classic database problem. I have an E-commerce software solution
This is a fairly common problem, it probably has a name, I just don't
This might not be possible but before I rewrite part of my application I
This is probably a common Objective-C question reported by Java coders, but I don't
This is probably going to be an underspecified question, as I'm not looking for
This is probably one of the most common tasks / problems when programming; You
I'm running into problems implementing statuses for a model. This is probably due to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.