2016-03-30 15 views
2

添付ファイル付きMFMailComposeViewControllerを使用すると、電子メールは送信トレイに残り、Air 2から送信されることはありません。iOS 9.3添付ファイルを含む電子メールは送信されません

//.h

@interface embEmailData : NSObject 
{ 
    NSArray  *to; 
    NSArray  *cc; 
    NSArray  *bcc; 
    NSString *subject; 
    NSString *body; 
    NSArray  *attachment; 
} 

@property (nonatomic, assign) BOOL optionsAlert; 

-(void)setTo:(NSArray*)to; 
-(NSArray*)to; 

-(void)setCc:(NSArray*)cc; 
-(NSArray*)cc; 

-(void)setBcc:(NSArray*)bcc; 
-(NSArray*)bcc; 

-(void)setSubject:(NSString*)subject; 
-(NSString*)subject; 

-(void)setBody:(NSString*)body; 
-(NSString*)body; 

-(void)setAttachment:(NSArray*)attachment; 
-(NSArray*)attachment; 

-(void)setOptionsAlert:(BOOL)options; 

//.m

#import "embEmailData.h" 
#import <MessageUI/MFMailComposeViewController.h> 
#import "UIImage+Utilities.h" 
@import MessageUI; 

#define kemailShowNSLogBOOL NO 

@interface embEmailData() <MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate> 
@property (nonatomic, strong) embEmailData  *receivedData; 
@property (nonatomic, strong) UIViewController *topVC; 
@end 

@implementation embEmailData 

- (id)init { 
    self = [super init]; 
    if (self) { 
     // Delay execution of my block for 0.1 seconds. 
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 
      [self postEmail]; 
     }); 

     _topVC = [UIApplication sharedApplication].keyWindow.rootViewController; 

    } 
    return self; 
} 

-(void)setTo:(NSArray*)_to 
{ 
    to = _to; 
    if (kemailShowNSLogBOOL) NSLog(@"%@",to); 
} 

-(NSArray*)to { 
    return to; 
} 

-(void)setCc:(NSArray*)_cc 
{ 
    cc = _cc; 
    if (kemailShowNSLogBOOL) NSLog(@"%@",cc); 
} 

-(NSArray*)cc { 
    return cc; 
} 

-(void)setBcc:(NSArray*)_bcc 
{ 
    bcc = _bcc; 
    if (kemailShowNSLogBOOL) NSLog(@"%@",bcc); 
} 

-(NSArray*)bcc { 
    return bcc; 
} 

-(void)setSubject:(NSString*)_subject 
{ 
    subject = _subject; 
    if (kemailShowNSLogBOOL) NSLog(@"%@",subject); 
} 

-(NSString*)subject { 
    return subject; 
} 

-(void)setBody:(NSString*)_body 
{ 
    body = _body; 
    if (kemailShowNSLogBOOL) NSLog(@"%@",body); 
} 

-(NSString*)body { 
    return body; 
} 

-(void)setAttachment:(NSArray*)_attachment 
{ 
    attachment = _attachment; 
    if (kemailShowNSLogBOOL) NSLog(@"%@",attachment); 
} 

-(NSArray*)attachment { 
    return attachment; 
} 

-(void)setOptionsAlert:(BOOL)options 
{ 
    if (options) 
    { 
     if (kemailShowNSLogBOOL) NSLog(@"options"); 
    } 
    else 
    { 
     if (kemailShowNSLogBOOL) NSLog(@"no options"); 
    } 

    _optionsAlert = options; 
} 

-(void)postEmail 
{ 
    _receivedData = self; 
    [self emailData]; 
} 

#pragma mark - Email Delegates 
-(void)emailData 
{ 
    if ([MFMailComposeViewController canSendMail] == YES) { 

     MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
     picker.mailComposeDelegate = self; // &lt;- very important step if you want feedbacks on what the user did with your email sheet 

     if(_receivedData.to) 
      [picker setToRecipients:_receivedData.to]; 

     if(_receivedData.cc) 
      for (NSString *email in _receivedData.cc) { 
       NSLog(@"cc email: %@",email); 
      } 
      [picker setCcRecipients:_receivedData.cc]; 

     if(_receivedData.bcc) 
      [picker setBccRecipients:_receivedData.bcc]; 

     if(_receivedData.subject) 
      [picker setSubject:_receivedData.subject]; 

     if(_receivedData.body) 
      [picker setMessageBody:_receivedData.body isHTML:NO]; // depends. Mostly YES, unless you want to send it as plain text (boring) 

     // attachment code 
     if(_receivedData.attachment) { 

      NSString *filePath; 
      NSString *justFileName; 
      NSData  *myData; 
      UIImage  *pngImage; 
      NSString *newname; 

      for (id file in _receivedData.attachment) 
      { 
       // check if it is a uiimage and handle 
       if ([file isKindOfClass:[UIImage class]]) { 
        NSLog(@"UIImage"); 

        myData = UIImagePNGRepresentation(file); 
        [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"image.png"]; 

        // might be nsdata for pdf 
       } else if ([file isKindOfClass:[NSData class]]) { 
        NSLog(@"pdf"); 
        myData = [NSData dataWithData:file]; 
        NSString *mimeType; 
        mimeType = @"application/pdf"; 
        newname = @"Westbrook Brochure.pdf"; 
        [picker addAttachmentData:myData mimeType:mimeType fileName:newname]; 

        // it must be another file type? 
       } else { 

        justFileName = [[file lastPathComponent] stringByDeletingPathExtension]; 

        NSString *mimeType; 
        // Determine the MIME type 
        if ([[file pathExtension] isEqualToString:@"jpg"]) { 
         mimeType = @"image/jpeg"; 
        } else if ([[file pathExtension] isEqualToString:@"png"]) { 
         mimeType = @"image/png"; 
         pngImage = [UIImage imageNamed:file]; 
        } else if ([[file pathExtension] isEqualToString:@"doc"]) { 
         mimeType = @"application/msword"; 
        } else if ([[file pathExtension] isEqualToString:@"ppt"]) { 
         mimeType = @"application/vnd.ms-powerpoint"; 
        } else if ([[file pathExtension] isEqualToString:@"html"]) { 
         mimeType = @"text/html"; 
        } else if ([[file pathExtension] isEqualToString:@"pdf"]) { 
         mimeType = @"application/pdf"; 
        } else if ([[file pathExtension] isEqualToString:@"com"]) { 
         mimeType = @"text/plain"; 
        } 

        filePath= [[NSBundle mainBundle] pathForResource:justFileName ofType:[file pathExtension]]; 
        NSLog(@"filePath %@ ",filePath); 

        UIImage * thumb = [UIImage imageNamed:filePath]; 
        UIImage * resizeThumb = [UIImage resizeImage:thumb withMaxDimension:999]; 

        if ([[file pathExtension] isEqualToString:@"pdf"]) { 

         //myData = [NSData dataWithData:file]; 
         myData = [[NSFileManager defaultManager] contentsAtPath:filePath]; 
         NSLog(@"ITS A PDF"); 
        } else // if it is anything but a png file 

         if (![[file pathExtension] isEqualToString:@"png"]) { 
          //myData = [NSData dataWithContentsOfFile:filePath]; 
          myData = UIImageJPEGRepresentation(resizeThumb, 1.0); 

         } else { 
          myData = UIImagePNGRepresentation(resizeThumb); 
         } 

        newname = file; 
        [picker addAttachmentData:myData mimeType:mimeType fileName:newname]; 

       } 
      } 
     } 

     picker.navigationBar.barStyle = UIBarStyleBlack; // choose your style, unfortunately, Translucent colors behave quirky. 
     [_topVC presentViewController:picker animated:YES completion:nil]; 

    } else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status" message:[NSString stringWithFormat:@"Email needs to be configured before this device can send email. \n\n Use [email protected] on a device capable of sending email."] 
                 delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [alert show]; 
    } 
} 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      break; 
     case MFMailComposeResultSaved: 
      break; 
     case MFMailComposeResultSent: 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you!" message:@"Email Sent Successfully" 
                  delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
      [alert show]; 
     } 
      break; 
     case MFMailComposeResultFailed: 
      break; 

     default: 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status" message:@"Sending Failed - Unknown Error" 
                  delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
      [alert show]; 
     } 
      break; 
    } 
    [_topVC dismissViewControllerAnimated:YES completion:nil]; 
} 

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result 
{ 
    NSLog(@"FINISHED"); 
} 
+0

私たちのiPadで同じ問題が発生しています。同じアプリは9.2.1で正常に動作します。私のPDFはわずか17Mbです。なぜそれがこれを行うのか分からない。 – johnslay

+0

1 pdfは5.8MB、もう1つは200kbです。 – malaki1974

+0

画像を送信しません... – malaki1974

答えて

-1

EDIT:それはまだ時々ランダムに私のためにこれを行います。私はそれらを削除するまで、Outboxに座っています。私は9.3.5に更新され、電子メールが送信されました。何が起こっているのか分からない。

自分のメールアカウントを私のデバイス(会社のGmailアカウント)から削除しようとしました。メール、カレンダー、連絡先、メモがONであることに気付きました。

削除してからもう一度追加すると、Notesはデフォルトでオフになり、自分のアプリからメールを送信できるようになりました。 YMMV。

関連する問題