首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MFMailComposeViewController问题

MFMailComposeViewController问题
EN

Stack Overflow用户
提问于 2009-12-16 18:46:04
回答 4查看 1.2K关注 0票数 0

这个问题可能不是MFMailComposeViewController特有的,但这正是我遇到问题的地方。

  • I正在为MFMailComposeViewController的messageBody构建NSString "myEmailString“,并将其存储在iVar中,然后将MFMailComposeViewController显示为模态视图控制器。
  • I将字符串传递到MFMailComposeViewController中,然后将其表示为模态视图MFMailComposeViewController--模态视图控制器被取消,my iVar无效,

当我在dealloc中发布iVar时,应用程序就崩溃了。

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

代码语言:javascript
复制
-(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];
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-12-16 18:53:51

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

要么你需要做

代码语言:javascript
复制
myEmailString = [[NSString stringWithString:buildString] retain];

代码语言:javascript
复制
self.myEmailString = [NSString stringWithString:buildString];

如果您将myEmailString声明为

代码语言:javascript
复制
@property (nonatomic, retain) NSString *myEmailString

想想看:如果所有的象牙都自动为你保留,那么你怎么会有一个你不想保留的变量呢?这就是为什么它不能那样工作的原因。

票数 3
EN

Stack Overflow用户

发布于 2009-12-16 18:52:54

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

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

那么您就可以安全地调用[myEmailString release]了。

票数 1
EN

Stack Overflow用户

发布于 2009-12-16 18:54:04

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1916783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档