发送短信分享和邮件分享

1.引入 MessageUI.framework

2.在要使用的.h文件中引入

#import <MessageUI/MessageUI.h>
#import<MessageUI/MFMailComposeViewController.h>
3.添加两个委托

MFMailComposeViewControllerDelegate

MFMessageComposeViewControllerDelegate


4.短信分享

//短信分享
- (IBAction)btn_share_sms_clicked:(id)sender 
{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    
    if (messageClass != nil) { 
        // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText]) {
            [self displaySMSComposerSheet];
        }
        else { 
            if (HUD.hidden) {
                HUD.hidden  = NO;
                HUD.labelText = @"该设备不支持短信功能";
                [HUD hide:YES afterDelay:2.0];
            }
        }
    }
    else {
    }
}

//信息分享,显示短信界面
-(void)displaySMSComposerSheet
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate =self;
        //要发送给谁
//        picker.recipients = [NSArray arrayWithObject:@"电话号码 "];
        NSString *smsBody =[NSString stringWithFormat:@"我分享了文件给您,地址是"];
        picker.body=smsBody;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }else{
        if (HUD.hidden) {
            HUD.hidden  = NO;
            HUD.labelText = @"该设备不支持短信功能";
            [HUD hide:YES afterDelay:2.0];
        }
    }
}

//反馈事件
- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissModalViewControllerAnimated:YES];
    switch ( result ) {
		case MessageComposeResultCancelled:
        {
			NSLog(@"点了短信界面取消按钮");
        }
			break;
		case MessageComposeResultFailed:
        {
            NSLog(@"发送失败");
        }
			break;
		case MessageComposeResultSent:
        {
			NSLog(@"发送成功");
        }
			break;
		default:
			break;
	}
}

5.邮件分享

//显示邮件的操作
-(void)displayMailComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    
    picker.mailComposeDelegate =self;
    //主题
    [picker setSubject:@"文件分享"];
    //收件人
    NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
    //抄送
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]", nil];
    //密送
    NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
    
    [picker setToRecipients:toRecipients];
    [picker setCcRecipients:ccRecipients];
    [picker setBccRecipients:bccRecipients];
    //发送图片附件
    //NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
    //NSData *myData = [NSData dataWithContentsOfFile:path];
    //[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy.jpg"];
    //发送txt文本附件
    //NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"];
    //NSData *myData = [NSData dataWithContentsOfFile:path];
    //[picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"];
    //发送doc文本附件 
    //NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];
    //NSData *myData = [NSData dataWithContentsOfFile:path];
    //[picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"];
    //发送pdf文档附件
    /*
     NSString *path = [[NSBundlemainBundle] pathForResource:@"CodeSigningGuide"ofType:@"pdf"];
     NSData *myData = [NSDatadataWithContentsOfFile:path];
     [pickeraddAttachmentData:myData mimeType:@"file/pdf"fileName:@"rainy.pdf"];
     */
    // Fill out the email body text
    //正文内容
    NSString *emailBody =[NSString stringWithFormat:@"我分享了文件给您,地址是"] ;
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
//邮件发送反馈
- (void)mailComposeController:(MFMailComposeViewController*)controller 
          didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    // Notifies users about errors associated with the interface
    switch (result)
    {
        caseMFMailComposeResultCancelled:
            NSLog(@"Result: Mail sending canceled");
            break;
        caseMFMailComposeResultSaved:
            NSLog(@"Result: Mail saved");
            break;
        caseMFMailComposeResultSent:
            NSLog(@"Result: Mail sent");
            break;
        caseMFMailComposeResultFailed:
            NSLog(@"Result: Mail sending failed");
            break;
        default:
            NSLog(@"Result: Mail not sent");
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

//邮件分享
- (IBAction)btn_share_email_clicked:(id)sender 
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    
    if (mailClass !=nil) {
        if ([mailClass canSendMail]) {
            [self displayMailComposerSheet];
        }else{
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }else{
        
    }
}


你可能感兴趣的:(Class,文档,Path,email,interface,sms)