iOS项目中如何隐藏AirPrint页面的打印与分享按钮?
iOS隐藏AirPrint页面的打印与分享按钮实现方案
在iOS开发中,系统默认的AirPrint弹窗(由UIPrintInteractionController提供)自带打印和分享按钮,要隐藏这两个按钮,有两种可行方案,分别适用于不同场景:
方案一:自定义打印预览与触发界面(推荐,无审核风险)
系统的UIPrintInteractionController的UI是固定不可直接修改的,因此最安全的方式是跳过系统弹窗,自行实现打印预览界面,仅保留你需要的交互控件,最后直接调用打印机选择或打印逻辑。
实现步骤与代码示例
- 配置打印任务基础信息:
UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = @"自定义打印任务"; UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController]; printController.printInfo = printInfo; // 设置打印内容(支持PDF、图片、自定义视图等) printController.printingItem = [NSURL fileURLWithPath:@"你的打印文件本地路径"];
- 创建自定义预览页面:
UIViewController *customPreviewVC = [[UIViewController alloc] init]; customPreviewVC.view.backgroundColor = [UIColor whiteColor]; // 添加预览视图(以PDF预览为例,使用WKWebView) WKWebView *previewWebView = [[WKWebView alloc] initWithFrame:customPreviewVC.view.bounds]; [previewWebView loadFileURL:printController.printingItem allowingReadAccessToURL:[printController.printingItem URLByDeletingLastPathComponent]]; [customPreviewVC.view addSubview:previewWebView]; // 添加自定义交互按钮(仅保留打印机选择功能) UIButton *selectPrinterBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, customPreviewVC.view.bounds.size.height - 80, customPreviewVC.view.bounds.size.width - 40, 50)]; [selectPrinterBtn setTitle:@"选择打印机" forState:UIControlStateNormal]; [selectPrinterBtn setBackgroundColor:[UIColor systemBlueColor]]; [selectPrinterBtn addTarget:self action:@selector(showPrinterSelection) forControlEvents:UIControlEventTouchUpInside]; [customPreviewVC.view addSubview:selectPrinterBtn]; // 展示自定义预览页 [self presentViewController:customPreviewVC animated:YES completion:nil];
- 实现打印机选择与打印触发:
- (void)showPrinterSelection { UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController]; // 仅展示打印机选择界面,不显示系统默认的打印/分享按钮 [printController presentFromRect:self.view.bounds inView:self.view animated:YES completionHandler:^(UIPrintInteractionController *_, BOOL completed, NSError *error) { if (completed) { NSLog(@"打印任务已提交"); } else if (error) { NSLog(@"打印失败: %@", error.localizedDescription); } }]; }
方案优势
- 完全自定义UI,符合App Store审核规范,无被拒风险;
- 可根据需求灵活添加或移除交互控件,适配业务场景。
方案二:通过Runtime修改系统AirPrint弹窗(谨慎使用,有审核风险)
如果不想自定义界面,可以通过遍历系统弹窗的子视图,找到打印和分享按钮并隐藏。但这种方法依赖系统私有UI元素的标识,可能随iOS版本更新失效,且存在App Store审核被拒的风险,仅适合企业内部应用或非上架场景。
实现代码示例
// 触发系统AirPrint弹窗 UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController]; printController.printInfo = printInfo; printController.printingItem = [NSURL fileURLWithPath:@"打印文件路径"]; [printController presentAnimated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) { // 弹窗展示后递归遍历子视图,隐藏目标按钮 [self hideTargetButtonsInView:printInteractionController.view]; }]; // 递归遍历并隐藏按钮 - (void)hideTargetButtonsInView:(UIView *)view { for (UIView *subview in view.subviews) { if ([subview isKindOfClass:[UIButton class]]) { UIButton *btn = (UIButton *)subview; // 通过accessibilityLabel识别打印/分享按钮(注意:不同iOS版本可能有变化) NSString *label = btn.accessibilityLabel; if ([label isEqualToString:@"Print"] || [label isEqualToString:@"Share"]) { btn.hidden = YES; } } // 递归处理子视图 [self hideTargetButtonsInView:subview]; } }
注意事项
- 系统控件的
accessibilityLabel可能随iOS版本更新改变,需要针对不同版本适配; - 直接修改系统私有UI元素违反苹果开发者规范,上架App Store时大概率会被拒绝。
内容的提问来源于stack exchange,提问作者Tapan Raut
相关产品推荐
相关产品推荐

