-.首先導入MessageUI.framework框架
二.導入頭文件#import
MFMessageComposeViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate
三. .h文件
#import#import @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UIButton *Email; @property (strong, nonatomic) IBOutlet UIButton *note; @property (strong, nonatomic) IBOutlet UIButton *camera; @property (strong, nonatomic) IBOutlet UIButton *mapDepot; - (IBAction)mailDelivery:(id)sender; - (IBAction)noteDelivery:(id)sender; - (IBAction)useTheCamera:(id)sender; - (IBAction)galleryUse:(id)sender;
四. .m文件
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(100, 20, 120, 120);
imageView.backgroundColor = [UIColor greenColor];
imageView.tag = 101;
[self.view addSubview:imageView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//發送郵件
- (IBAction)mailDelivery:(id)sender {
// 判斷設備是否是否可以發送郵件
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
NSString * feedbackMsg;
if (!mailClass) {
feedbackMsg = @"當前系統版本不支持應用內發送郵件功能";
UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:@"提示"
message:feedbackMsg
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alertView show];
}else if(![mailClass canSendMail])
{
feedbackMsg = @"您沒有設置郵件賬戶";
UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:@"提示"
message:feedbackMsg
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alertView show];
}else{
[self sendFeedBackMail];
}
}
//發送短信
- (IBAction)noteDelivery:(id)sender {
BOOL judge = [MFMessageComposeViewController canSendText];
if (judge) {
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc]init];
picker.messageComposeDelegate = self;
//收件人
picker.recipients = @[@"186*******"];
//內容
picker.body = @"很高興見到你!";
[self presentViewController:picker animated:YES completion:nil];
}else
{
UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:@"提示"
message:@"系統不具備此項功能!"
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alertView show];
}
}
//使用相機功能
- (IBAction)useTheCamera:(id)sender {
// UIImagePickerControllerCameraDeviceRear 後置攝像頭
// UIImagePickerControllerCameraDeviceFront 前置攝像頭
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (!isCamera) {
NSLog(@"沒有攝像頭");
return ;
}
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
// 編輯模式
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
//使用圖庫
- (IBAction)galleryUse:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
#pragma mark--------------使用相機----------------
// 選中照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"%@", info);
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:101];
// UIImagePickerControllerOriginalImage 原始圖片
// UIImagePickerControllerEditedImage 編輯後圖片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
// 取消相冊
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark--------------發送短信----------------
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
NSString *title = @"郵件發送提醒";
NSString *msg;
switch (result) {
case MessageComposeResultCancelled:
msg = @"短信取消!";
[self alertWithTitle:title msg:msg];
break;
case MessageComposeResultSent:
msg = @"短信成功發送!";
[self alertWithTitle:title msg:msg];
break;
case MessageComposeResultFailed:
msg = @"短信發送失敗!";
[self alertWithTitle:title msg:msg];
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark--------------發送郵件----------------
-(void)sendFeedBackMail
{
//是否創建郵件
BOOL judge = [MFMailComposeViewController canSendMail];
if (judge) {
//創建郵件
MFMailComposeViewController *pick = [[MFMailComposeViewController alloc]init];
//標題
[pick setSubject:@"意見反饋"];
//郵件地址
NSArray *address = [NSArray arrayWithObject:@"請填上自己的郵箱"];
[pick setToRecipients:address];
//內容
NSString *message = @"你好!";
pick.mailComposeDelegate = self;
[pick setMessageBody:message isHTML:NO];
//返回
[self presentViewController:pick animated:YES completion:NULL];
}
}
//代理
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *title = @"郵件發送提醒";
NSString *msg;
switch (result) {
case MFMailComposeResultCancelled:
msg = @"郵件取消!";
[self alertWithTitle:title msg:msg];
break;
case MFMailComposeResultSaved:
msg = @"郵件保存成功!";
[self alertWithTitle:title msg:msg];
break;
case MFMailComposeResultSent:
msg = @"郵件發送成功!";
[self alertWithTitle:title msg:msg];
break;
case MFMailComposeResultFailed:
msg =@"郵件發送失敗!";
[self alertWithTitle:title msg:msg];
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)alertWithTitle:(NSString *)_title_ msg:(NSString *)msg{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
message:msg
delegate:nil
cancelButtonTitle:@"好"
otherButtonTitles:nil];
[alert show];
}