iOS-調用系統的短信和發送郵件功能,實現短信分享和郵件分享


一、郵件分享

1、iOS系統自帶郵件設置郵箱(此處以QQ郵箱為例)(http://jingyan.baidu.com/album/6181c3e084cb7d152ef153b5.html?picindex=1)

  注意:由於QQ郵箱激活時間不滿14天,暫時無法設置POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務

     會造成只能發送郵件,但是郵件接收不到的情況。

2、實現功能

- (void)displayMailComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    
    // 設置picker的委托方法,完成之后會自動調用成功或失敗的方法
    picker.mailComposeDelegate = self;
    // 添加主題
    [picker setSubject:@"文件分享"];
    // 添加收件人
    //NSArray *toRecipients = [NSArray arrayWithObject:@"收件人@qq.com"];
    // 說明:也可以添加多個收件人,代碼如下所示:
//    NSArray *toRecipients = [NSArray arrayWithObjects:@"one@qq.com",@"two@qq.com",nil];
    // 添加抄送
//    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@qq.com",@"third@qq.com", nil];
    // 添加密送
//    NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@qq.com"];
    
    [picker setToRecipients:toRecipients];
//    [picker setCcRecipients:ccRecipients];
//    [picker setBccRecipients:bccRecipients];
    
    // 直接在HTML代碼中寫入圖片的地址
    NSString *emailBody = [NSString stringWithFormat:@"<img src='http://p2.so.qhimg.com/t0130e3288d86929b97.jpg' /><p>我分享了圖片</p>"];
    
    [picker setMessageBody:emailBody isHTML:YES];
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

這樣實現的效果是,在郵件中會顯示一個圖片和一段文本,關鍵是第25行代碼,一定要設置isHTML為YES。但實際應用中,圖片一般我們沒有上傳到服務器上,而是在客戶端,這時候我們可以用下面的方式來發送圖片(將第23行代碼替換成下面的代碼):

// 發送圖片附件(其他格式的附件,可以都先轉化稱NSData類型,然后設置相應的mimeType即可,如txt類型為@"text/txt",doc類型為@"text/doc",pdf類型為@"file/pdf"等等)
    NSData *myData = [NSData dataWithContentsOfFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"new.png"]];
    [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"new.png"];
    NSString *emailBody = [NSString stringWithFormat:@"<p>我分享了圖片</p>"];

使用上面這種方式,將isHTML設置為YES的話,圖片會顯示在正文中;isHTML設置為NO的話,圖片會顯示在附件中。

》添加郵箱附件方法:

//發送圖片附件
//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:myDatamimeType:@"file/pdf"fileName:@"rainy.pdf"];
*/

 

郵件發送完成之后,可以在如下的委托方法中進行相應的處理,委托方法如下所示:

- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: Mail sending canceled");  // 郵件發送取消  
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: Mail saved");  // 郵件保存成功
            break;
        case MFMailComposeResultSent:
            NSLog(@"Result: Mail sent");  // 郵件發送成功
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: Mail sending failed");  // 郵件發送失敗
            break;
        default:
            NSLog(@"Result: Mail not sent");
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

導入MessageUI.framework

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

#import<MessageUI/MFMailComposeViewController.h>

實現 MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate

 

二、發送短信

//短信
-(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:pickeranimated:YES];
[pickerrelease];
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM