I'm testing on an iPod Touch running OS 3.1.3
Trying to allow users to send an email from within the app - but when the following code is executed, the 开发者_StackOverflowentire screen just turns completely blank / white.
Any ideas on why this is happening? I've got the MessageUI framework in the project. I'm importing and delegating in the header file:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
<MFMailComposeViewControllerDelegate>
And here's the code, pretty standard:
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"App Feedback"];
[picker setToRecipients:[NSArray arrayWithObject:@"xyz@gmail.com"]];
[self presentModalViewController:picker animated:YES];
[picker release];
}
And then I have the didFinishWithResult function that would dismiss the ModalViewController when the email has been sent.
But again, all I get is a blank white screen on my iPod Touch. =/
Thanks!
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setSubject:@"App Feedback"];
[mail setMessageBody:@"*your message content*" isHTML:NO];
[self presentModalViewController:mail animated:YES];
[mail release];
}
- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}
You can take look at sample code from apple: http://developer.apple.com/library/ios/#samplecode/MessageComposer/Listings/Classes_MessageComposerViewController_m.html
-(IBAction)showMailPicker:(id)sender {
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
[self displayMailComposerSheet];
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.";
}
}
-(void)displayMailComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self;
[picker setSubject:@"Hello from California!"];
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];
NSString *emailBody = @"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
} - (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];
}
精彩评论