2012-02-11 14 views
0

私は電子メールで単一の電子メールアドレスに送信したいテキストフィールド(5フィールド)からなる連絡フォームを持っています。 xCodeでこれをどうやって行うのですか?お問い合わせフォームを電子メールで送信

+1

はすでに[SO](http://stackoverflow.com/questions/7087199/xcode前の答え-4-ios-send-an-email-using-my-app-inside-my-app) –

答えて

0

リンクされた投稿にも同様の回答がありますが、canSendMailが既にチェックされているので、私のコードを追加しています。私はまた、電子メールに他のものを追加するのを容易にするコメント付きコードを残しました。あなたが唯一の私は、このコードを使用する無料アプリ、QCountを、持っている

のiOS 5をターゲットにしている場合、これは、実質的に容易であること

注意。確かに、私はあなたの.hで、私はhttp://itunes.apple.com/ng/app/qcount/id480084223?mt=8

お楽しみください:-)私のコピー&ペーストからカスタムのすべてを剥奪

ダミアン

を願っています:あなたの中

#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:@"[email protected]", nil]; 
// NSArray *ccRecipients = [[NSArray alloc] init]; 
// NSArray *bccRecipients = [[NSArray alloc] init]; 
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
// NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", 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:[email protected][email protected],[email protected]&subject=Hello from California!"; 
NSString *body = @"&body=Feedback"; 

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; 
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; 
} 
1

この問題を抱えている人は、 iOSのコンタクトフォームにドロップインします。

これは私のニーズによく合っており、PHPコンポーネントを使用して実際に電子メールを送信します。 。(例えば、スクリプトはサンプルプロジェクトに含まれている

私はここのGithubにそれを投稿:

https://github.com/mikecheckDev/MDContactForm

+0

これを共有してくれてありがとう、それは素晴らしいです!:-) – kernix

関連する問題