您的位置:首页 > 移动开发 > IOS开发

小胖说事8------iOS 调用系统的短信和发送邮件功能,实现短信分享邮件分享

2015-05-12 15:05 786 查看
导入MessageUI.framework

.h文件中#import <MessageUI/MessageUI.h>

#import<MessageUI/MFMailComposeViewController.h>

实现 MFMailComposeViewControllerDelegate,

MFMessageComposeViewControllerDelegate

.m 文件

//邮件
-(void)showMailPicker {
   Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    
if (mailClass !=nil) {
        if ([mailClass canSendMail]) {
[selfdisplayMailComposerSheet];
}else{
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }else{
        
    }
    
}
-(void)displayMailComposerSheet 
{
MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc] init];
   
picker.mailComposeDelegate =self;
[pickersetSubject:@"文件分享"];
// Set up recipients
NSArray *toRecipients = [NSArrayarrayWithObject:@"first@qq.com"]; 
NSArray *ccRecipients = [NSArrayarrayWithObjects:@"second@qq.com",@"third@qq.com", nil]; 
NSArray *bccRecipients = [NSArrayarrayWithObject:@"fourth@qq.com"]; 
    
[pickersetToRecipients:toRecipients];
[pickersetCcRecipients:ccRecipients];
[pickersetBccRecipients: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 =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address] ;
[pickersetMessageBody:emailBody isHTML:NO];
[selfpresentModalViewController:picker animated:YES];
[pickerrelease];
}
- (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;
}
[selfdismissModalViewControllerAnimated:YES];
}


//短信
-(void)showSMSPicker{
   Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));

    if (messageClass != nil) { 
   // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText]) {
        [selfdisplaySMSComposerSheet];
        }
        else { 
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持短信功能" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
            
        }
    }
    else {
        }
 }
-(void)displaySMSComposerSheet
{
   MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc] init];
picker.messageComposeDelegate =self;
NSString *smsBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address] ;
    picker.body=smsBody;
[selfpresentModalViewController:picker animated:YES];
[pickerrelease];
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self.rootViewController dismissViewControllerAnimated:YES completion:nil];
    //Notifies users about errors associated with the interface
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SMSSendFinished" object:nil];
    switch (result) {
            
        case MessageComposeResultCancelled:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:nil message:@"发送取消" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [msgbox show];
            [msgbox release];
        }
            break;
            
        case MessageComposeResultSent:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:nil message:@"发送成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [msgbox show];
            [msgbox release];
        }
            break;
            
        case MessageComposeResultFailed:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:nil message:@"发送失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [msgbox show];
            [msgbox release];
        }
            break;
            
        default:
            
            break;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: