ios – 向UIActivityController添加自定义活动时出现问题

前端之家收集整理的这篇文章主要介绍了ios – 向UIActivityController添加自定义活动时出现问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试对标准活动(Print,Mail,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’取消注释该行,我只获得系统Print图标.

我欢迎任何意见,以帮助我搞清楚这一点.

如果有人知道任何示例代码来做我想要的,那将是非常棒的.

编辑:更新了我的工作代码代码.

解决方法

在过去的这篇文章中,我正在把头发拉过UIActivity,苹果确实需要更好地解释它并添加更多功能;试试这个:

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

别忘了制作这两个文件

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,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];
}
原文链接:https://www.f2er.com/iOS/334182.html

猜你在找的iOS相关文章