MFMailComposeViewController问题

时间:2009-12-16 18:46:05

标签: iphone iphone-sdk-3.0

这个问题可能并非特定于MFMailComposeViewController,但这就是我遇到问题的地方......

  • 我正在构建NSString “myEmailString”为messageBody MFMailComposeViewController和 之前将它存储在iVar中 显示 MFMailComposeViewController作为 模态视图控制器。

  • 我将字符串传递给MFMailComposeViewController,然后将其显示为模态视图控制器。

  • 当模态视图控制器被解除时,我的iVar变为无效,
    当我在dealloc

  • 中发布emailString iVar时,应用程序崩溃了

下面的代码,我做错了什么?

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

4 个答案:

答案 0 :(得分:3)

你的伊娃宣布怎么样?它被宣布为财产吗?在任何情况下,它都不会自动保留给您。

要么你需要做

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

self.myEmailString = [NSString stringWithString:buildString];

如果你将myEmailString声明为

@property (nonatomic, retain) NSString *myEmailString

考虑一下:如果所有的ivars都是自动保留的,那么你将如何拥有想要保留的变量?这就是为什么它不起作用的原因。

答案 1 :(得分:1)

当你在buildEmailMessage中创建myEmail字符串时,你永远不会保留字符串。因此,在离开该功能后,它是自动释放的。那么当调用dealloc时你的保留计数将为0,这将导致崩溃。如果你想保留变量,你需要有如下行

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

然后你可以安全地致电[myEmailString release]

答案 2 :(得分:1)

stringWithString:创建一个新字符串并在将其返回给您之前自动释放它。除非保留返回的字符串,否则不需要在dealloc方法中释放它。

答案 3 :(得分:1)

在将字符串存储到iVar中之前,您应该保留字符串:

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

没有它会变得无效,因为它会在程序执行期间稍后自动释放。这也将确保在调用析构函数时仍然分配它,以防止发布崩溃。