Set default subject when click on email address

让人想犯罪 __ 提交于 2019-12-04 06:46:09

问题


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?


回答1:


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!




回答2:


    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];
}


来源:https://stackoverflow.com/questions/10104159/set-default-subject-when-click-on-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!