8

我正在尝试对标准活动(打印、邮件、FaceBook 等)实施自定义活动,但现在只需要标准打印(用于 AirPrint)和我自己通过直接方法进行的自定义打印。我显然缺少一些基本的东西,因为我的自定义类中的任何方法都没有被调用过。现在我只有一些 NSLog 语句来确定调用顺序,并让框架运行。

以下是我对自定义活动类的测试代码:

//  PrintActivity.h

#import <UIKit/UIKit.h>

@interface PrintActivity : UIActivity

@end

还有他们

#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

}

这是主程序中的调用:

- (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];
    }

正如我所说,没有调用自定义类中的任何方法,但系统邮件、消息和复制图标显示在活动表中,而不是打印图标。我只希望系统打印图标(和我自己的)。

如果我取消注释顶部的语句块(并注释掉 NSArray *activityItems ......),则系统邮件、消息、打印和复制图标。在这个实验中,我认为我通过创建自己的格式化程序来混合不同的方法,但这似乎是 WWDC 2012 的建议?

如果我随后取消注释带有“excludeActivityTypes”的行,我只会得到系统打印图标。

我欢迎任何意见来帮助我解决这个问题。

如果有人知道任何示例代码来做我想做的事,那就太好了。

编辑:将代码更新为我的工作代码。

4

3 回答 3

7

在过去的一周里,我也在 UIActivity 上大发雷霆,Apple 确实需要更好地解释它并添加更多功能;尝试这个:

打印活动.h

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

打印活动.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

不要忘记也制作这两个文件:

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  

现在我们终于可以调用它了:

- (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];
}
于 2012-10-07T18:18:17.640 回答
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!!
}
于 2013-03-21T21:46:18.613 回答
-1

谢谢你的帮助,伙计们!而且,对于活动图像,我们似乎必须在 Apple 文档之后提供图像:

图像的 alpha 通道用作掩码以生成呈现给用户的最终图像。图像本身中的任何颜色数据都将被忽略。不透明的像素应用了一个渐变,然后这个渐变被放置在标准背景的顶部。因此,一个完全不透明的图像会产生一个渐变填充的矩形。对于 iPhone 和 iPod touch,图像不应大于 43 x 43 点(对于配备 Retina 显示屏的设备,这相当于 86 x 86 像素。)对于 iPad,图像不应大于 55 x 55 点(相当于 110 x对于配备 Retina 显示屏的 iPad,为 110 像素。)

于 2012-11-21T07:05:52.707 回答