Set default subject when click on email address

前端 未结 2 787
遥遥无期
遥遥无期 2021-01-27 16:19

I am using dataDetectorTypes property with UITextView.code works fine.

When I click on link email composer appears with pre-filled To:[email address] but i want to set d

相关标签:
2条回答
  • 2021-01-27 16:45
        MFMailComposeViewController *mailController =    [[MFMailComposeViewController alloc] init]; 
    
       mailController.mailComposeDelegate = self;
    
    if([MFMailComposeViewController canSendMail]){
        [mailController setSubject:@"Subject"];
        [mailController setMessageBody:@"Email body here" isHTML:NO]; 
        [mailController setMessageBody:[self getInFo] isHTML:YES]; 
        [mailController setToRecipients:[NSArray arrayWithObject:@"xx@xxx.com"]];
        [mailController setTitle:@"Title"];
    
        [self presentModalViewController:mailController animated:YES]; 
    }else{
        [mailController release];
    }
    
    0 讨论(0)
  • 2021-01-27 17:07

    1)Fist add <UITextViewDelegate, MFMailComposeViewControllerDelegate> to the class that contains the textview.

    You must add two imports to this class:

    #import <MessageUI/MessageUI.h>
    #import <MessageUI/MFMailComposeViewController.h>
    

    2) Add a variable: MFMailComposeViewController (in this example mailVC, you can also add it as a class property in your .h)

    3) Implement the next method:

    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
    

    This allows to intercepts the specific url interaction. You can cancel a specific interaction and add your own action, for example:

    -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
       //EXAMPLE CODE
        if ([[URL scheme] isEqualToString:@"mailto"]) {
    
            mailVC = [[MFMailComposeViewController alloc] init];
            [mailVC setToRecipients:@[@"your@destinationMail.com"]];
            [mailVC setSubject:@"A subject"];
            mailVC.mailComposeDelegate = self;
    
            [self presentViewController:mailVC animated:YES completion:^{
               // [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
            }];
            return NO;
        }
        return YES;
    }
    

    3) To dismiss your MFMailComposeViewController variable:

    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
        [mailVC dismissViewControllerAnimated:YES completion:nil];
    }
    

    That works for me!

    0 讨论(0)
提交回复
热议问题