我是iphone开发的新手。我正在构建一个应用程序,通过它我可以发送电子邮件。我正在使用以下代码
- (IBAction)btnsendPressAction:(id)sender {
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mailer=[[MFMailComposeViewController alloc]init];
mailer.mailComposeDelegate=self;
[mailer setSubject:@"A Message for testing"];
NSArray *toRecipients = [NSArray arrayWithObjects:@"rajshetty2125@gmail.com", @"secondMail@example.com", nil];
[mailer setToRecipients:toRecipients];
UIImage *image=[UIImage imageNamed:@"logo.png"];
NSData *imageData=UIImagePNGRepresentation(image);
[mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"logo.png"];
NSString *emailBody=@"This is test email";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
mailer.modalPresentationStyle = UIModalPresentationPageSheet ;
}
else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"failure" message:@"Alert" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
我的问题是mailComposeController委托没有被调用,我无法通过我的app发送电子邮件。我经历了很多链接,但我找到了相同的方法。任何人都可以帮我吗?
答案 0 :(得分:2)
您将无法从模拟器发送电子邮件。您只能使用邮件应用程序中配置的任何帐户在iOS设备上检查此功能。
答案 1 :(得分:0)
此处使用此link并在设备中添加(已配置)帐户,然后才能发送邮件。
答案 2 :(得分:0)
这段代码对我有用:
if ([MFMailComposeViewController canSendMail]) {
self.mailController = [[MFMailComposeViewController alloc] init];
[self.mailController setSubject:@"Invitation"];
[self.mailController setMessageBody:@"Message" isHTML:NO];
self.mailController.mailComposeDelegate = self;
[self presentModalViewController:self.mailController animated:YES];
}
但请确保您在接口
中实现委托@interface MyClass() <MFMailComposeViewControllerDelegate>
并检查您的回调方法:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}
答案 3 :(得分:-1)
嘿,我不确定,但可能有帮助,替换你的最后一行
mailer.modalPresentationStyle = UIModalPresentationPageSheet ;
带
[self presentModalViewController: mailer animated:YES];