Problems adding custom activity to UIActivityController
Asked Answered
C

3

8

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.

Cuvette answered 5/10, 2012 at 18:19 Comment(0)
P
7

I was pulling my hair out over UIActivity as well this past tweek, it really needs to be explained better by Apple and have more features added; Try this:

PrintActivity.h

#import <UIKit/UIKit.h>
@interface PrintActivity : UIActivity
@end

PrintActivity.m

#import "PrintActivity.h"

@implementation PrintActivity

- (NSString *)activityType
{
   return @"MetriScan.Print";
}

- (NSString *)activityTitle
{
    return @"Print MtriScan";
}

- (UIImage *)activityImage
{   
    //***** Note: I recommend using two sizes, as the iPad's UIActivity image size differs from 
    //***** the iPhone's. Also, create @2x sizes for Retina compatible devices. So you will     
    //***** have a total of 4 images.
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    return [UIImage imageNamed:@"test_72.png"];
}

    return [UIImage imageNamed:@"test_57.png"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    NSLog(@"%s", __FUNCTION__);
    return YES;
}

- (void)prepareWithActivityItems:(NSArray *)activityItems
{
    NSLog(@"%s",__FUNCTION__);
}

- (UIViewController *)activityViewController
{
    NSLog(@"%s",__FUNCTION__);
    return nil;
}

- (void)performActivity
{
    // This is where your custom print code should go

}

@end

Don't forget to make these two files as well:

PrintProvider.h

#import <UIKit/UIKit.h>

@interface PrintProvider : UIActivityItemProvider <UIActivityItemSource>

@end

PrintProvider.m

#import "PrintProvider.h"

@implementation PrintProvider

#pragma mark - UIActivityItemSource

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
    NSLog(@"%s",__FUNCTION__);
    NSLog(@"%@", activityType);
    return [super activityViewController:activityViewController itemForActivityType:activityType];
}

@end  

Now we can finally call it:

- (IBAction)printReport:(UIBarButtonItem *)sender {


CustomProvider *customProvider =
                [[CustomProvider alloc]init];
                NSArray *items = [NSArray arrayWithObjects:customProvider,nil];

                CustomActivity *ca = [[CustomActivity alloc]init];

                UIActivityViewController *activityVC =
                [[UIActivityViewController alloc] initWithActivityItems:items
                                                  applicationActivities:[NSArray arrayWithObject:ca]];

                activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo,
                UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard,
                UIActivityTypeSaveToCameraRoll,UIActivityTypeMail,UIActivityTypePostToTwitter,
                UIActivityTypePostToFacebook,UIActivityTypeMessage];

                activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
                {
                    NSLog(@" activityType: %@", activityType);
                    NSLog(@" completed: %i", completed);
                };

                    self.popoverController = [[UIPopoverController alloc] initWithContentViewController:activityVC];

                    CGRect rect = [[UIScreen mainScreen] bounds];

                    [self.popoverController
                     presentPopoverFromRect:rect inView:self.view
                     permittedArrowDirections:0
                     animated:YES];
}
Polonium answered 7/10, 2012 at 18:18 Comment(3)
troop231 thank you for answering the question. It turns out I had solved the problem almost identically to you. My original confusing was what to put in activityItems and applicationActivities, respectively. For completeness I have updated the code in my original question.Cuvette
You're welcome :) I think the potential for UIActivity is huge.Polonium
Thanks, it really works. But doesn't this approach contradict to Apple warning about not override system services? You should subclass UIActivity only if you want to provide custom services to the user. The system already provides support for many standard services and makes them available through the UIActivityViewController object. For example, the standard activity view controller supports emailing data, posting items to one of the user’s social media accounts, and several other options. You do not have to provide custom services for any of the built-in types.Langlois
A
3

@troop231 - great answer, and very helpful.

The only thing I'd add is to be sure to signal completion of the operation or the completion routine won't get called and the popover won't dismiss. Something like:

- (void)performActivity {
    NSLog(@"Do the actual activity here");
    // My custom code here

    [self activityDidFinish:YES];  // indicate completion here!!
}
Almagest answered 21/3, 2013 at 21:46 Comment(2)
Thank you for adding this. I have been wondering about the proper way. This will cause activityViewController.completionHandler to fire, where you should dismiss avc.Hair
I just had a thought, but haven't had time to test it. It MIGHT be true that it's unnecessary to explicitly call the completion handler (as I showed above) if one (instead) called [super performActivity] before returning. Is it possible the super class method would invoke the completion handler? Let me know if you have time to try it.Almagest
G
-1

thanks for your help, guys! And, for the activity image, it seems we have to provide image following this by Apple docs:

The alpha channel of the image is used as a mask to generate the final image that is presented to the user. Any color data in the image itself is ignored. Opaque pixels have a gradient applied to them and this gradient is then laid on top of a standard background. Thus, a completely opaque image would yield a gradient filled rectangle. For iPhone and iPod touch, images should be no larger than 43 by 43 points (which equates to 86 by 86 pixels for devices with Retina displays.) For iPad, images should be no larger than 55 x 55 points (which equates to 110 by 110 pixels for iPads with Retina displays.)

Godding answered 21/11, 2012 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.