アプリ内でメールを起動する方法。
FrameworksにMessageUI.frameworkを追加
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
MFMailComposeViewControllerDelegateを指定
- (void)mailAction:(id)sender{
//メール設定・送信可能かを確認する
if( ![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"メール設定"
message:@"メール起動に失敗しました。メールの設定をご確認ください"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
//[picker setSubject:@"件名だよ"];
//[picker setToRecipients:[NSArray arrayWithObjects:@"test@gmail.com"]];
//[picker setCcRecipients:[NSArray arrayWithObject:@"CC MailID"]];
//[picker setBccRecipients:[NSArray arrayWithObject:@"BCC Mail ID"]];
// Fill out the email body text
NSString *emailBody = @"body_message";
// This is not an HTML formatted email
[picker setMessageBody:emailBody isHTML:NO];
// Create NSData object as PNG image data from camera image
NSData *data = UIImagePNGRepresentation(imageData);
[picker addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage"];
// Show email view
[self presentModalViewController:picker animated:YES];
}
これで画像が添付できます~。
そして送信したら閉じる⬇
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
if(result ==MFMailComposeResultFailed){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"メール送信"
message:@"メールの送信に失敗しました。ネットワークの設定などを確認して下さい"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
/*switch (result){
case MFMailComposeResultCancelled:
//キャンセルした場合
break;
case MFMailComposeResultSaved:
//保存した場合
break;
case MFMailComposeResultSent:
//送信した場合
break;
case MFMailComposeResultFailed:
break;
}*/
[self dismissModalViewControllerAnimated:YES];
}
言語設定はアプリケーションのplistファイルにて、「Localization native development region」を「Japan」を設定すると日本語表記になります。
今までメールは
[ [ UIApplication sharedApplication ] openURL:[ NSURL URLWithString:@"mailto:test@example.com" ] ];
使ってたけど、アプリ内でメール起動できた方がいいね^^
FrameworksにMessageUI.frameworkを追加
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
MFMailComposeViewControllerDelegateを指定
- (void)mailAction:(id)sender{
//メール設定・送信可能かを確認する
if( ![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"メール設定"
message:@"メール起動に失敗しました。メールの設定をご確認ください"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
//[picker setSubject:@"件名だよ"];
//[picker setToRecipients:[NSArray arrayWithObjects:@"test@gmail.com"]];
//[picker setCcRecipients:[NSArray arrayWithObject:@"CC MailID"]];
//[picker setBccRecipients:[NSArray arrayWithObject:@"BCC Mail ID"]];
// Fill out the email body text
NSString *emailBody = @"body_message";
// This is not an HTML formatted email
[picker setMessageBody:emailBody isHTML:NO];
// Create NSData object as PNG image data from camera image
NSData *data = UIImagePNGRepresentation(imageData);
[picker addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage"];
// Show email view
[self presentModalViewController:picker animated:YES];
}
これで画像が添付できます~。
そして送信したら閉じる⬇
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
if(result ==MFMailComposeResultFailed){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"メール送信"
message:@"メールの送信に失敗しました。ネットワークの設定などを確認して下さい"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
/*switch (result){
case MFMailComposeResultCancelled:
//キャンセルした場合
break;
case MFMailComposeResultSaved:
//保存した場合
break;
case MFMailComposeResultSent:
//送信した場合
break;
case MFMailComposeResultFailed:
break;
}*/
[self dismissModalViewControllerAnimated:YES];
}
言語設定はアプリケーションのplistファイルにて、「Localization native development region」を「Japan」を設定すると日本語表記になります。
今までメールは
[ [ UIApplication sharedApplication ] openURL:[ NSURL URLWithString:@"mailto:test@example.com" ] ];
使ってたけど、アプリ内でメール起動できた方がいいね^^