IOS development of shared mail

  Include this and a full-screen shot of UIWebView , can be used together, the first of UIWebView screenshot and share to the mailbox (to do as well as micro letter to share the time, Tencent Weibo, Sina microblogging function, these three According to official data, relatively easy to implement , there is not a commentary carried).

  Below are listed first source for message transmission function:

. 1 - ( void ) displayMailComposerSheet
 2  {
 . 3      the MFMailComposeViewController * picker = [[the MFMailComposeViewController the alloc] the init];
 . 4      
. 5      // delegate picker is provided a method, after completion of the success or failure will automatically invoke a method 
. 6      picker.mailComposeDelegate = Self;
 . 7      / / add topics 
8      [Picker setSubject: @ " file sharing " ];
 9      // add recipients 
10      NSArray * toRecipients = [NSArray arrayWithObject: @ " [email protected] " ];
 11      // Note: You can also add multiple recipients, code as follows:
12 is  //     NSArray toRecipients * = [NSArray arrayWithObjects: @ "[email protected]", @ "[email protected]", nil];
 13 is      // Add Cc
 14  //     NSArray ccRecipients * = [NSArray arrayWithObjects: @ "[email protected]", @ "[email protected]", nil];
 15      // Add Bcc
 16  //     NSArray bccRecipients * = [NSArray arrayWithObject: @ "[email protected]"]; 
. 17      
18 is      [ setToRecipients Picker: toRecipients];
 . 19  //     [Picker setCcRecipients: ccRecipients];
 20 is  //     [Picker setBccRecipients: bccRecipients];
 21 is      
22 is      // write the address of the image directly in the HTML code 
23     NSString *emailBody = [NSString stringWithFormat:@"<img src='http://p2.so.qhimg.com/t0130e3288d86929b97.jpg' /><p>我分享了图片</p>"];
24     
25     [picker setMessageBody:emailBody isHTML:YES];
26     [self presentModalViewController:picker animated:YES];
27     [picker release];
28 }

  The effect is achieved, it will show a picture and a text in the message, the key is the first 25 lines of code, be sure to set isHTML to YES . However, practical applications, generally we do not have pictures uploaded to the server, but the client, this time we can use the following ways to send pictures (replace the first 23 lines of code to the following code):

1      // send picture attachments (attachments other formats can be converted, said NSData are the first type, and then set the appropriate mimeType can, such as txt type @ "text / txt", doc type @ "text / doc", pdf type @ "file / pdf", etc.) 
2      NSData myData * = [NSData dataWithContentsOfFile: [[NSHomeDirectory () stringByAppendingPathComponent: @ " Documents " ] stringByAppendingPathComponent: @ " new.png " ]];
 . 3      [Picker addAttachmentData: mimeType myData : @ " Image / jpeg " fileName: @ " new.png " ];
 4      NSString * emailBody = [NSString stringWithFormat: @ " <the p-> I share a picture </ the p-> " ];

  With the above in this manner, the isHTML set to YES, then the image will be displayed in the text; isHTML set to NO, then images will be displayed in the annex.

  After completion message is sent, the corresponding processing can be performed following methods delegate, the delegate method as follows:

 1 - (void)mailComposeController:(MFMailComposeViewController*)controller
 2           didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
 3 {
 4     switch (result)
 5     {
 6         case MFMailComposeResultCancelled:
 7             NSLog(@"Result: Mail sending canceled");  // 邮件发送取消  
 8             break;
 9         case MFMailComposeResultSaved:
10             NSLog(@"Result: Mail saved");   // message successfully saved 
11              BREAK ;
 12          Case MFMailComposeResultSent:
 13              NSLog ( @ " the Result: Sent Mail " );   // message sent successfully 
14              BREAK ;
 15          Case MFMailComposeResultFailed:
 16              NSLog ( @ " the Result: Mail sending failed The " ) ;   // Mail failed to send 
17              BREAK ;
 18          default :
 19              NSLog ( @ " the Result: not sent Mail " );
 20             break;
21     }
22     [self dismissModalViewControllerAnimated:YES];
23 }

  Send e-mail to call the function code is as follows:

 1     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
 2     
 3     if (mailClass !=nil)
 4     {
 5         if ([mailClass canSendMail])
 6         {
 7             [self displayMailComposerSheet];
 8         }
 9         else
10         {
11             UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"不支持邮件功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
12             [alert show];
13             [alert release];
14         }
15     }
16     else
17     {
18         
19     }

  [Description]: using the mail transmission function, you need to introduce MessageUI.framework frame, and add the following headers:

  #import <MessageUI/MessageUI.h>

  #import <MessageUI/MFMailComposeViewController.h>

  Implement the interface MFMailComposeViewControllerDelegate.

Reproduced in: https: //www.cnblogs.com/eagle927183/p/3476480.html

Guess you like

Origin blog.csdn.net/weixin_33711647/article/details/93725172