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

转载请保留cml_cml~~~~~~

---------------------------------------------------------------------------------------------------------------1------------------------------------------------------------------------------------------------------------------

先用Storyboard拖四个按钮,与对应的controller类关联属性和方法


---------------------------------------------------------------------------------------------------------------发送邮件:--------------------------------------------------------------------------------------------------

导入MessageUI.framework

遵循MFMailComposeViewControllerDelegate协议

//发送邮件
- (IBAction)mailDelivery:(id)sender {
    //  判断设备是否是否可以发送邮件
    //  NSClassFromString简单介绍http://blog.csdn.net/cml_cml/article/details/37659421
    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];
    }
}
-(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];
}


---------------------------------------------------------------------------------------------------------------发送短信------------------------------------------------------------------------------------------------------

导入MessageUI.framework

遵循MFMessageComposeViewControllerDelegate协议

//发送短信
- (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];

        
    }
}
#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];
}


//  alertWithTitle:msg:方法与邮箱的一样

---------------------------------------------------------------------------------------------------------------相机使用------------------------------------------------------------------------------------------------------

导入UIImagePickerControllerDelegate

//使用相机功能
- (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:^{
    }];
}
使用
UIImagePickerControllerDelegate
UINavigationControllerDelegate代理方法
#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];   
}


---------------------------------------------------------------------------------------------------------------5------------------------------------------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值