I'm trying to implement the usual activities for standard actions (Print, Mail, FaceBook, etc.), but at the moment you only need the standard Print (for AirPrint) and my own direct printing method. I obviously missed something fundamental, since none of the methods of my user class were ever called. At the moment, I only have an NSLog instruction to figure out the sequence of calls and make the frame function.
Below is my test code for a custom activity class:
// PrintActivity.h #import <UIKit/UIKit.h> @interface PrintActivity : UIActivity @end
And .m
#import "PrintActivity.h" @interface PrintActivity () @property (nonatomic, strong) UIWebView *dummyWebView; @end @implementation PrintActivity - (NSString *)activityType { NSLog(@"activityType"); return @"MetriScan Print"; } - (NSString *)activityTitle { NSLog(@"activityTitle"); return @"MetriScan\nPrint"; } - (UIImage *)activityImage { NSLog(@"activityImage"); UIImage *icon = [UIImage imageNamed:@"metriscan_57_c2a_3.png"]; return icon; } - (BOOL)canPerformWithActivityItems:(NSArray *)activityItems { NSLog(@"canPerformWithActivityItems"); return YES; } - (void)prepareWithActivityItems:(NSArray *)activityItems { NSLog(@"prepareWithActivityItems"); } - (void)performActivity { NSLog(@"Do the actual printing here"); // My custom code here }
And this is the call in the main procedure:
- (IBAction)printReport:(UIBarButtonItem *)sender { NSLog(@"Print Report"); PrintActivity *metriscanPrint = [[PrintActivity alloc] init]; UIViewPrintFormatter *printFormatter = [self.webView viewPrintFormatter]; NSArray *activityItems = [NSArray arrayWithObjects:printFormatter, nil]; NSArray *appActivities = [NSArray arrayWithObjects:metriscanPrint, nil]; UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:appActivities];
As I said, none of the methods in the user class are called, but the system mail, message, and copy icons appear on the activity sheet, not on the Print icon. I expected that there would only be a "Print System" icon (and my own).
If I uncomment the top block of statements (and comment out NSArayArray * activityItems ...........) further down, the Mail, Message, Print and Copy systme icons. In this experiment, I think I'm mixing different methods, creating my own formatter, but this seemed to be a proposal at WWDC 2012?
If I uncomment the line with "excludeActivityTypes", I get only the "Print System" icon.
I would like to welcome any materials that will help me understand this.
And if anyone knows any sample code to do what I want, that would be awesome.
Edit: Updated code for my working code.