Set default subject when click on email address

前端 未结 2 789
遥遥无期
遥遥无期 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 17:07

    1)Fist add to the class that contains the textview.

    You must add two imports to this class:

    #import 
    #import 
    

    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!

提交回复
热议问题