ios:添加打印,但保持与ios 3的兼容性

发布于 2024-10-04 19:23:05 字数 866 浏览 3 评论 0原文

我正在尝试向 iOS 应用程序添加打印功能。 虽然打印本身工作正常,并且该应用程序可以在 ios 上运行> 4,我还没有弄清楚如何保持ios 3.1兼容性...

我猜问题是这样的:completionHandler:(UIPrintInteractionCompletionHandler)

UIPrintInteractionCompletionHandler 类型的块,您实现它来处理 打印作业结束(例如,重置状态)并 处理打印中遇到的任何错误。

一旦我添加块:

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
};

该应用程序甚至不会在 iOS 3.1 上启动 可能是因为那里没有可用的块。

是的,我确保这段代码在 iOS 3.1 上启动时不会运行...

if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.2) && ([UIPrintInteractionController isPrintingAvailable]))

所以我想知道是否有办法为 iOS > 4.2 提供打印支持,但保持它在 iOS 3.1 上运行?

也许有一种方法可以使用方法而不是“块”? 或者如何在受支持的 iOS 设备上提供打印功能,并保持向后兼容 iOS 3.1?

i'm trying to add printing features to an ios app.
while printing itself works fine, and the app works on ios > 4, i haven't figured out yet how to keep the ios 3.1 compatibility...

i guess the issue is this: completionHandler:(UIPrintInteractionCompletionHandler)

A block of type UIPrintInteractionCompletionHandler that you implement to handle the
conclusion of the print job (for instance, to reset state) and to
handle any errors encountered in printing.

once i add the block:

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
};

the app won't even launch on iOS 3.1
probably because blocks aren't available there.

yes, i made sure that this code won't be run when launched on iOS 3.1...

if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.2) && ([UIPrintInteractionController isPrintingAvailable]))

so i wonder if there's a way to have printing support for iOS >4.2, but keeping it to run on iOS 3.1?

maybe there's a way to use a method instead of the "block"?
or how would be the correct way to have printing available on supported iOS devices, and remain backwards compatible to iOS 3.1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亢潮 2024-10-11 19:23:05

只需将 -weak_framework UIKit 添加到“其他链接器标志”下的项目设置中,并确保使用条件代码来打印 API。
条件代码应该检查功能可用性,而不是操作系统版本:

    if (NSClassFromString(@"UIPrintInteractionController")){
    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    };
}

将项目目标设置为 iOS 3,然后就可以开始了。

just add -weak_framework UIKit to the project settings under "Other Linker Flags" and make sure you use conditional code for printing API.
Conditional code should check feature availability, not OS version:

    if (NSClassFromString(@"UIPrintInteractionController")){
    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    };
}

Set your project target to iOS 3, and you're good to go.

七婞 2024-10-11 19:23:05

检测 AirPrint 是否可用的最佳实践是使用 NSClassFromString。如果您通常使用此方法,那么您总是知道您想要的类是否可用,而不必硬编码哪些功能与哪个版本相对应。示例代码:

 Class printControllerClass = NSClassFromString(@"UIPrintInteractionController");
 if (printControllerClass) {
   [self setupCanPrintUI];
 } else {
   [self setupCannotPrintUI];
 }

这样您的应用程序仍然可以在以前的 iOS 版本上运行,尽管它无法从它们进行打印。

我已经能够使用这种技术并在 iOS 3.0 设备上运行它,而块代码(基于 ^ 的东西)没有任何问题。在我的构建设置中,我将基本 SDK 设置为 iOS 4.2,将部署目标设置为 iOS 3.0。

我在这篇关于 iOS 打印的博文的末尾发布了一个示例 Xcode 项目。这是我在 iOS 3.0 设备和 iOS 4.2 设备上成功运行的项目。您可能必须更改 info.plist 中的包标识符才能使代码签名为您工作,但这与打印内容无关。

The best practice for detecting if AirPrint is available is to use NSClassFromString. If you use this method in general, then you always know if exactly the class you want is available, without having to hard-code which features correspond with which version. Example code:

 Class printControllerClass = NSClassFromString(@"UIPrintInteractionController");
 if (printControllerClass) {
   [self setupCanPrintUI];
 } else {
   [self setupCannotPrintUI];
 }

That way your app can still work on previous iOS versions, although it won't be able to print from them.

I've been able to use this technique and run it on an iOS 3.0 device without any problems with the block code (the ^-based stuff). In my build settings, I have the Base SDK set to iOS 4.2, and the Deployment Target set to iOS 3.0.

I posted a sample Xcode project at the end of this blog post on printing in iOS. This is the project that successfully runs for me on a device with iOS 3.0 and another device with iOS 4.2. You may have to change the bundle identifier in the info.plist to get the code-signing to work for you, but that's independent of the printing stuff.

錯遇了你 2024-10-11 19:23:05

将项目设置中的部署目标设置为 iOS 3.x。但是,将 Base SDK 设置为 4.2。现在您可以使用 4.2 类,运行 3.x 的 iPhone 也可以安装您的应用程序。
请记住,当您在 iPhone 3.x 上使用 4.2 类时,应用程序将会崩溃(因此请不断检查系统版本)。

Set Deployment Target in your Project Settings to iOS 3.x. However, set the Base SDK to 4.2. Now you can use the 4.2 classes and iPhones running 3.x can install your app too.
Keep in mind that when you use a 4.2 class on an iPhone 3.x, the application will crash (so keep checking the system version on-the-go).

很快妥协 2024-10-11 19:23:05
NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare:@"3.2" options: NSNumericSearch];
  if (order == NSOrderedSame || order == NSOrderedDescending && [[UIDevice currentDevice]isMultitaskingSupported]) {
   // >4.2
  }
               else {
                       //< 4.2
                }

注意:
还将 UIKit 框架设置从“必需”更改为“弱”,这将帮助您在 iOs < 上运行应用程序4.2 以及 iOs >= 4.2

NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare:@"3.2" options: NSNumericSearch];
  if (order == NSOrderedSame || order == NSOrderedDescending && [[UIDevice currentDevice]isMultitaskingSupported]) {
   // >4.2
  }
               else {
                       //< 4.2
                }

Note:
also change UIKit framework setting from "required" to "weak" this will help you to run application on iOs < 4.2 as well as iOs >= 4.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文