gpt4 book ai didi

ios - 将自定义 Activity 添加到 UIActivityController 时出现问题

转载 作者:可可西里 更新时间:2023-11-01 03:09:09 26 4
gpt4 key购买 nike

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

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

//  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

}

这是主例程中的调用:

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

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

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

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

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

如果有人知道任何示例代码来执行我想要的操作,那就太好了。

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

最佳答案

在过去的一周里,我也对 UIActivity 感到不安,Apple 确实需要更好地解释它并添加更多功能;试试这个:

打印 Activity .h

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

打印 Activity .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];
}

关于ios - 将自定义 Activity 添加到 UIActivityController 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12751891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com