邮件、短信、相机、图库的使用

-.首先导入MessageUI.framework框架

二.导入头文件#import <MessageUI/MessageUI.h>和代理方FMailComposeViewControllerDelegate,

MFMessageComposeViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate

三. .h文件

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

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@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];
}


posted @ 2019-06-03 12:37  ldxsuanfa  阅读(123)  评论(0编辑  收藏  举报