Problems adding custom activity to UIActivityController

前端 未结 3 1944
遥遥无期
遥遥无期 2020-12-10 06:23

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

相关标签:
3条回答
  • 2020-12-10 06:57

    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.)

    0 讨论(0)
  • 2020-12-10 07:07

    @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!!
    }
    
    0 讨论(0)
  • 2020-12-10 07:12

    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];
    }
    
    0 讨论(0)
提交回复
热议问题