简体   繁体   中英

MFMailComposeViewController only presented after second tap

I want to present a MFMailComposeViewController from a modal view controller. Basically this method works, but it does not work reliably:

-(void)sendMailTapped:(id)sender{

    [self resetButtonsStateAfterTapping:sender];
    [self dismissPopover];

    if (filesize>10) {
        [self showAlertForExceededMaximumAttachmentSize];
        return;
    }

    @try {
        MFMailComposeViewController *picker = 
                                  [[MFMailComposeViewController alloc] init];
        if ([MFMailComposeViewController canSendMail]) {
            picker.mailComposeDelegate = self;
            NSURL *path =[NSURL urlWithPath:[pageInfoDict valueForKey:@"file_name"] 
                                      docId:[pageInfoDict valueForKey:@"id_doc"]
                                  encrypted:[[pageInfoDict valueForKey:@"encrypted"] 
                                                                         boolValue]] ;
            NSString *fileName = [pageInfoDict valueForKey:@"title"];
            if([fileName length] == 0) {
                fileName = [path lastPathComponent];
            }
            if(![fileName hasSuffix:[path pathExtension]]){
                fileName=[fileName stringByAppendingFormat:@".%@",[path pathExtension]];
            }

            [picker setSubject:[@"Send document: " stringByAppendingString:fileName]];

            NSArray *ccRecipients = [NSArray arrayWithObjects: 
                                  [[CustomisationConfig getAppConfig] getCCMail],nil];
            [picker setCcRecipients:ccRecipients];

            NSArray *bccRecipients = [NSArray arrayWithObjects:
                                   [[CustomisationConfig getAppConfig] getBCCMail],nil];
            [picker setBccRecipients:bccRecipients];

            NSData *myData = [path decryptedData];
            [picker addAttachmentData:myData 
                                  mimeType:fileMIMEType(fileName) fileName:fileName];

            NSString *emailBody = [NSString stringWithFormat:
                                  @"\n\nThis file was sent using %@.", 
                                  [DCConfiguration getHumanReadableAppName]  ];
            [picker setMessageBody:emailBody isHTML:NO];

            [self presentViewController:picker animated:true completion:^(void){}];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"ContextMenuViewController sendMailTapped:%@",exception.description);
    }      
}

If I restart my iPad and open the app, the picker will be presented only on the second tap on the corresponding button.
If I click again on the button after this, the picker will be presented on first touch everytime and it works perfectly, until I shutdown and restart the iPad.

The following is printed to the console:

Warning: Attempt to present <MFMailComposeViewController: 0x200cbb70> on <ContextMenuViewController: 0x200cd1a0> whose view is not in the window hierarchy!

What is calling the sendMailTapped: method? If its interface builder, you need to change it to IBAction instead of void , and connect them in interface builder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM