テキスト フィールド (5 つのフィールド) で構成されたコンタクト フォームを 1 つの電子メール アドレスに電子メールで送信したいと考えています。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 に答える