Iphone 如何使用MFMailComposeViewController设置收件人
我正在尝试获取我选择发送电子邮件的电子邮件。 但是我不知道如何设置我在MFMailComposeViewController视图中选择的用户Iphone 如何使用MFMailComposeViewController设置收件人,iphone,objective-c,xcode,mfmailcomposeviewcontroller,Iphone,Objective C,Xcode,Mfmailcomposeviewcontroller,我正在尝试获取我选择发送电子邮件的电子邮件。 但是我不知道如何设置我在MFMailComposeViewController视图中选择的用户 if ([MFMailComposeViewController canSendMail]) { mailer = [[MFMailComposeViewController alloc] init]; mailer.mailComposeDelegate = self; [mailer setSu
if ([MFMailComposeViewController canSendMail])
{
mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"A Message from blablabl"];
NSMutableArray *usersTo = [[NSMutableArray alloc] init];
toRecipients = usersTo;
[mailer setToRecipients:toRecipients];
NSString *emailBody = @"blablablabal";
[mailer setMessageBody:emailBody isHTML:YES];
// only for iPad
// mailer.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:mailer animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
委派
这里有几件事不对 (一) MFMailComposeViewController的方法需要已设置收件人的不可变数组 (二) 将其设置为一个空的可变数组 试着这样做:
NSArray *usersTo = [NSArray arrayWithObject: @"nobody@stackoverflow.com"];
[mailer setToRecipients:usersTo];
您应该看到它的工作。MFMailComposer在“Swift”和“Objective c”中
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
NSArray *toRecipients = [NSArray arrayWithObjects:@"first@gmail.com",@"second@gmail.com",nil];
[picker setToRecipients:toRecipients];
*********************************************
在目标c中
步骤:
1.创建新项目。
2.在故事板中添加一个按钮。
3.在.h中添加头文件,如“导入”
4.将委托添加到ViewController“MFMailComposeViewController delegate”
5.按钮动作{
NSString*emailTitle=“”
NSString*messageBody=“”
NSArray*ToRecipients=“”
MFMailComposeViewController*VC=[[MFMailComposeViewController]alloc init];
VC.mailComposeDelegate=self;
[VC.setSubject:emailTitle];
[VC.setMessageBody:messageBody];
[VC.SetToRecipients:ToRecipients];
如果([MFMailComposeViewController canSendMail]){
[自我呈现视图控制器:mc动画:是完成:空];
}
6.MailComposeController功能
-(void)mailComposeController:(MFMailComposeViewController*)控制器未完成结果:(MFMailComposeResult)结果错误:(NSError*)错误
{
开关(结果)
{
案例MFMailComposer结果已取消:
NSLog(@“邮件已取消”);
打破
案例MFMailComposer结果已保存:
NSLog(@“已保存邮件”);
打破
案例MFMailComposer结果已发送:
NSLog(@“已发送邮件”);
打破
案例MFMailComposer结果失败:
NSLog(@“邮件发送失败:%@,[错误本地化描述]);
打破
违约:
打破
}
[self-dismissViewControllerAnimated:是完成:空];
}
7.同时添加框架:MessageUI.FrameWork
8.将应用程序运行到手机或模拟器中。
迅速:
*******
步骤:
1.创建新项目
2.添加按钮故事板
3.在ViewController.Swift类文件导入消息UI中
4.以下步骤中配置的按钮操作MailComposer
{
让mailComposeViewController=configuredMailComposeViewController()
如果MFMailComposeViewController.canSendMail()
{
self.present(mailComposeViewController,动画:true,完成:nil)
self.showsendMailSuccessfulAlert()
}否则{
self.showSendMailErrorAlert()
}
5.实现配置邮件生成器
func配置了mailcomposeviewcontroller()->MFMailComposeViewController{
让mailComposerVC=MFMailComposeViewController()
mailComposerVC.mailComposeDelegate=self
MailComposer VC.setToRecipients([“someone@somewhere.com"])
mailComposerVC.setSubject(“向您发送应用程序内电子邮件…”)
setMessageBody(“在应用程序中发送电子邮件还不错!”,isHTML:false)
返回邮件
}
6.MailComposer可选方法
func-mailcomosecontroller(u控制器:mfmailcomoseviewcontroller,didFinishWith结果:MFMailComposeResult,错误:error?){
控制器。解除(动画:true,完成:nil)
}
7.完成步骤编号后:将应用程序运行到设备或模拟器中。
它不起作用……”nobody@stackoverflow.com&1更多\U2026“,只能收到一封电子邮件,因为在他放置&n个用户之后…我将尝试调整uitextfield@MichaelDautermann在使用IOS模拟器在Xcode中测试应用程序时无法发送电子邮件,因为IOS模拟器中的设置中缺少邮件应用程序。当我将Iphone设备连接到Mac,并在此d上从Xcode运行应用程序时设备,有没有一种方法可以在Mac上显示我的Iphone屏幕,这样我就可以用鼠标控制在物理设备上运行的应用程序,并从Mac上显示的屏幕发送电子邮件,而不是触摸Iphone设备本身的屏幕?许多感谢API可以使预定义的收件人成为只读的?这样用户就无法访问添加新收件人?@SazzadHissainKhan我不知道!苹果可能更希望用户能够控制代表用户发送电子邮件的位置。
MFMailComposer In “ Swift “ and “ Objective c “
*********************************************
In objective c
Steps:
1. Create new project.
2. Add a button into storyboard.
3. In .h add header file like “Import <MessageUI/MessageUI.h>”
4. Add delegate to ViewController “ MFMailComposeViewControllerDelegate”
5. In Button Action {
NSString *emailTitle = “”
NSString *messageBody = “”
NSArray *toRecipents = “”
MFMailComposeViewController *VC = [[MFMailComposeViewController]alloc init];
VC.mailComposeDelegate = self;
[VC.setSubject: emailTitle];
[VC.setMessageBody: messageBody];
[VC.setToRecepents: toRecipents];
if([MFMailComposeViewController canSendMail]) {
[self presentViewController:mc animated:YES completion:NULL];
}
6.MailComposeController Function
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
7. And also add FrameWork: MessageUI.FrameWork
8. Run the App into mobile or Simulator.
In Swift:
*******
Steps:
1. Create New Project
2. Add a button Storyboard
3. In ViewController.Swift Class File Import MessageUI
4. In Button Action ConfiguredMailComposer below Steps
{
let mailComposeViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail()
{
self.present(mailComposeViewController, animated: true, completion: nil)
self.showSendmailSuccesfulAlert()
} else {
self.showSendMailErrorAlert()
}
5. Implement the configure mail composer
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["someone@somewhere.com"])
mailComposerVC.setSubject("Sending you an in-app e-mail...")
mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)
return mailComposerVC
}
6. MailComposer optional method
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
7. After completed the step no: run the app into device or simulator.