OC - 邮件发送

OC - 邮件发送 首页 / iOS入门教程 / OC - 邮件发送

无涯教程可以使用iOS设备的电子邮件应用程序发送电子邮件。

涉及步骤

步骤1 - 创建一个简单的基于视图的应用程序。

步骤2 - 选择您的项目文件,然后选择目标,然后添加 MessageUI.framework 。

无涯教程网

步骤3 - 在 ViewController.xib 中添加一个按钮,并创建用于发送电子邮件的操作。

步骤4 - 如下更新 ViewController.h -

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate> {
   MFMailComposeViewController *mailComposer;
}

-(IBAction)sendMail:(id)sender;

@end

步骤5 - 如下更新 ViewController.m -

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   //Dispose of any resources that can be recreated.
}

-(void)sendMail:(id)sender {
   mailComposer=[[MFMailComposeViewController alloc]init];
   mailComposer.mailComposeDelegate=self;
   [mailComposer setSubject:@"Test mail"];
   [mailComposer setMessageBody:@"Testing message 
   for the test mail" isHTML:NO];
   [self presentModalViewController:mailComposer animated:YES];
}

#pragma mark - mail compose delegate
-(void)mailComposeController:(MFMailComposeViewController *)controller 
   didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
   
   if (result) {
      NSLog(@"Result : %d"result);
   }
   
   if (error) {
      NSLog(@"Error : %@"error);
   }
   
   [self dismissModalViewControllerAnimated:YES];
}
@end

运行应用程序时,将获得以下输出-

iOS Tutorial

点击发送电子邮件后,无涯教程将获得以下输出-

iOS Tutorial

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

硅谷产品实战36讲 -〔曲晓音〕

10x程序员工作法 -〔郑晔〕

零基础学Java -〔臧萌〕

SQL必知必会 -〔陈旸〕

Go 语言项目开发实战 -〔孔令飞〕

HarmonyOS快速入门与实战 -〔QCon+案例研习社〕

说透元宇宙 -〔方军〕

超级访谈:对话玉伯 -〔玉伯〕

深入拆解消息队列47讲 -〔许文强〕

好记忆不如烂笔头。留下您的足迹吧 :)