问题添加自定义活动UIActivityController [英] Problems adding custom activity to UIActivityController

查看:411
本文介绍了问题添加自定义活动UIActivityController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个自定义活动标准的活动(打印,邮件,Facebook等),但现在只需要在标准打印(适用于AirPrint的),并通过直接的方法我自己的自定义打印。我显然失去了一些东西根本是没有在我的自定义类中的方法不断被调用。现在我只是有一些的NSLog语句来找出调用序列,并获得该框架的功能。

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.

以下是我的测试code为自定义活动类:

The following is my test code for the custom activity class:

//  PrintActivity.h

#import <UIKit/UIKit.h>

@interface PrintActivity : UIActivity

@end

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

如果我去掉语句的顶块(并注释掉的NSArray * activityItems ...........)进一步回落,systme邮件,邮件,打印和复制图标。在这个实验中,我认为我是混合使用不同的方法,通过创建自己的格式,但是这似乎是暗示在WWDC 2012?

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 ?

如果我再取消注释与'excludeActivityTypes行我只得到了系统打印图标。

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.

如果有人知道任何例如code做什么,我想,这将是了不起的。

And if anybody know of any example code to do what I want, that would be terrific.

编辑:更新code到我的工作code

Updated code to my working code.

推荐答案

我拉我的头发了UIActivity以及过去的tweek,它真的需要苹果来解释更好,拥有更多功能的加入;试试这个:

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

PrintActivity.h

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

PrintActivity.m

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

PrintProvider.h

#import <UIKit/UIKit.h>

@interface PrintProvider : UIActivityItemProvider <UIActivityItemSource>

@end

PrintProvider.m

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

这篇关于问题添加自定义活动UIActivityController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆