0

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 default Subject:[subject string] also.

How can I do this?

Akshay Nalawade
  • 1,447
  • 2
  • 14
  • 29

2 Answers2

2

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!

VictorPurMar
  • 151
  • 9
1
    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];
}
mhunturk
  • 296
  • 2
  • 12
  • This is not what i am looking for. I want to add subject when I try to send mail by clicking on email in uitextview and dataDetectorTypes property is set to UIDataDetectorTypeAll. – Akshay Nalawade Apr 11 '12 at 11:33