我有一个由文本字段(5 个字段)组成的联系表格,我想通过电子邮件将其发送到一个电子邮件地址。我如何在 xCode 中执行此操作?
问问题
4122 次
2 回答
1
对于遇到此问题的任何人,您都可以使用这个插入式 iOS 联系表。
这很符合我的需求,它使用 PHP 组件来实际发送电子邮件。(示例项目中包含一个示例脚本。
我在这里将其发布到 Github:
于 2012-08-05T04:01:13.703 回答
0
链接的帖子有类似的答案,但我正在添加我的代码,因为它已经检查了 canSendMail。我还留下了一堆注释代码,可以轻松地将其他内容添加到电子邮件中。
请注意,如果您只针对 iOS 5,这要容易得多。
我有一个使用此代码的免费应用程序 QCount。确实,我希望我从我的复制粘贴中删除了所有自定义内容:-) http://itunes.apple.com/ng/app/qcount/id480084223?mt=8
享受,
达米安
在您的 .h 中:
#import <MessageUI/MessageUI.h>
.m 中的方法:
- (void)emailLabelPressed { // or whatever invokes your email
// Create a mail message in the user's preferred mail client
// by opening a mailto URL. The extended mailto URL format
// is documented by RFC 2368 and is supported by Mail.app
// and other modern mail clients.
//
// This routine's prototype makes it easy to connect it as
// the action of a user interface object in Interface Builder.
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];
}
}
-(void)displayComposerSheet {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Your Form Subject"];
// Take screenshot and attach (optional, obv.)
UIImage *aScreenshot = [self screenshot];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(aScreenshot)];
[picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"screenshot"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"first@example.com", nil];
// NSArray *ccRecipients = [[NSArray alloc] init];
// NSArray *bccRecipients = [[NSArray alloc] init];
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
// NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
[picker setToRecipients:toRecipients];
// [picker setCcRecipients:ccRecipients];
// [picker setBccRecipients:bccRecipients];
// Attach an image to the email.
/* NSString *path = [[NSBundle mainBundle] pathForResource:@"ipodnano"
ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png"
fileName:@"ipodnano"];
*/
// Fill out the email body text.
// NSString *emailBody = @"Use this for fixed content.";
NSMutableString *emailBody = [[NSMutableString alloc] init];
[emailBody setString: @"Feedback"];
// programmatically add your 5 fields of content here.
[picker setMessageBody:emailBody isHTML:NO];
// Present the mail composition interface.
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
[self presentViewController:picker animated:YES completion:nil];
} else {
[self presentModalViewController:picker animated:YES];
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error {
if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self dismissModalViewControllerAnimated:YES];
}
}
-(void)launchMailAppOnDevice {
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
NSString *body = @"&body=Feedback";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
于 2012-02-11T02:05:47.520 回答