Disable editing on MFMailComposeViewController

后端 未结 4 677
温柔的废话
温柔的废话 2021-01-06 21:38

We have a business rule where we need to restrict the editing of the email body content on MFMailComposeViewController. The content will be pre-populated and needs to remain

4条回答
  •  甜味超标
    2021-01-06 22:37

    Ok, so yes, the answer is that it is impossible without using private APIs.

    I managed to do it with the following code.

    - (void) getMFComposeBodyFieldViewFromView:(UIView *)view {
       for (UIView *item in view.subviews) {
        if ([[[item class] description] isEqualToString:@"MFComposeTextContentView"]) {
             self.mailBodyView = item;
             break;
          }
          if([item.subviews count] > 0) {
            [self getMFComposeBodyFieldViewFromView:item];
          }
       }
    }
    

    Then call the above method so that it sets the ivar mailBodyView and then call the _setEditable: method on UIWebDocumentView which MFComposeBodyField inherits from.

    [self getMFComposeBodyFieldViewFromView:mailComposeViewController.view];
    [self.mailBodyView setEditable:NO];
    

    This causes the content in the body to be uneditable, the interaction is kind of funky because the keyboard still appears and you can still move the cursor around and select things but it definitely prevents the user from editing.

    UPDATE

    I updated the code to look for MFComposeTextContentView which is the parent of MFComposeBodyField and I call setEditable: on that object which prevents the keyboard from coming up, much better solution!

提交回复
热议问题