0
votes

I created a subclass of NSObject with the purpose of using instances of this class in other classes to execute the creation of a pdf file and then attaching and emailing that file. This should keep from having the heavy lifting of pdf creation and emailing code replicated all over the app. Here is the h file:

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


@interface PBVemailWithAttachment : NSObject <MFMailComposeViewControllerDelegate>

//call to create a new PDF file using UIGraphicsBeginPDFContextToFile with the specifeid width and    
height
- (void)setupPDFDocumentNamed:(NSString*)name Width:(float)width Height:(float)height;

//call to start a new page in an existing PDF document wit the size provided in pageRect.
- (void)beginPDFPageWithRect: (CGRect)pageRect;

//call to add text to the current pdf page given a specific font and font size and drawn a the given      
frame.  returns the frame in whcih the text is drawn in order to allow subsequent calls to position   
additional elements on the page correctly relvant to the previously added text.
- (CGRect)addText:(NSString*)text withFont:(NSString*)font withFrame:(CGRect)frame fontSize:
(float)fontSize;


//call to draw a line on the page with the given frame and in the given color.  Returns the frame of the 
drawn line for positioning subsequent objects on the page.

- (CGRect)addLineWithFrame:(CGRect)frame withColor:(UIColor*)color;

//call to place an image on the page at a specific point.  A CGRect will be created for the image frame    
using the CGpoint and the width and height of the provided image.  Returns the frame Rect of the image  
to popsition subsequent objects on the page.

- (CGRect)addImage:(UIImage*)image atPoint:(CGPoint)point;



//closes the UIGraphics content being used to generate the PDF file

- (void)finishPDF;


-(void)emailFileNameAsAttachment:(NSString *)filename withSubject: (NSString *)subject withMessageBody:   
(NSString *)messageBody withMIMEType: (NSString *)mimeType AttachmentNamed:(NSString *)attachmentName;

@end

Problem is in the .m file I get an error implementing the delegate method saying the dismissViewControlelrAnimated is not a visibly declared selector. Now I realize this is because I have subclassed NSObject. So I have a couple of questions on how to get this done:

  1. Should I subclass UIViewConotrller or NSObject?
  2. And probably more importantly and which may answer 1, where do I implement the MFMailCompose Delegate, on the custom object OR in the class that will call an instance of this object and call its methods to do the "heavy lifting" of creating the PDF and attaching ti and emailing it out?

    -(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(
    MFMailComposeResult )result error: (NSError*)error {

    [self dismissViewControllerAnimated:YES completion:nil]; }

1

1 Answers

0
votes

Just to close this out, I ended up creating a custom object to handle the pdf creation but calling the email handler methods directly in the class. I still have a general question about this if anyone has an answer: Is it possible to create custom objects to handle "heavy lifting of repetitive tasks" if the custom object will implement a delegate method? I am still unclear of the pattern.