I am trying to implement a custom activity to the standard activities (Print, Mail, FaceBook, etc.) but for now only want the standard Print (for AirPrint) and my own custom printing by a direct method. I am obviously missing something fundamental as none of the methods in my custom class ever get called. For now I only have some NSLog statement to figure out the calling sequence, and to get the framework to function.
The following is my test code for the custom activity class:
// PrintActivity.h
#import <UIKit/UIKit.h>
@interface PrintActivity : UIActivity
@end
And the .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 invocation in the main routine:
- (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];
//activityController.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo, UIActivityTypeMail, UIActivityTypeMessage, nil];
activityController.completionHandler = ^(NSString *activityType, BOOL completed) {
sender.enabled = YES;
};
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self presentViewController:activityController animated:YES completion:nil];
} else {
sender.enabled = NO;
self.printPop = [[UIPopoverController alloc] initWithContentViewController:activityController];
[self.printPop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
As I said, none of the methods in the custom class gets called, but the system Mail, Message, and Copy icons show in the activity sheet and not the Print icon. I expected just the system Print icon (and my own).
If I uncomment the top block of statements (and comment out the NSArray *activityItems ...........) further down, the systme Mail, Message, Print, and Copy icons. In this experiment I think I am mixing different methods by creating my own formatter, but that seemed to be the suggestion at WWDC 2012 ?
If I then uncomment the line with 'excludeActivityTypes' I only get the system Print icon.
I would welcome any input to help me figuring this out.
And if anybody know of any example code to do what I want, that would be terrific.
Edit: Updated code to my working code.