4

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?

1
  • 1
    the short answer: don't use LLVM compiler 1.6. 1) your project’s Base SDK must be iOS 4.2 (or greater), 2) your project’s Deployment Target must be iOS 3.1 or greater. (so you need to give up pre-3.1 iPhone compatibility.), 3) your project’s C/C++ Compiler Version must be LLVM GCC 4.2 (if you need support for pre-4.0 iOS) or LLVM compiler 1.6 (if you only support iOS 4.0 and above, because it won’t weak-link support for blocks). ----/ found this answer in a post of Nov 22 2010: marco.org
    – jra
    Nov 30, 2010 at 7:57

4 Answers 4

6

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.

5
  • thanks a lot! it certainly can be solved with the build configuration... my issue was, that i had LLVM compiler 1.6 set [it won’t weak-link support for blocks]... and it works perfectly fine with LLVM GCC 4.2. see also: marco.org (entry of Nov 22 2010)
    – jra
    Nov 30, 2010 at 7:55
  • You still can't refer to UIPrint... classes though, as they don't exist in iOS 3.x. How did you get around this? Dec 8, 2010 at 2:53
  • objektivs, NSClassFromString is only used to detect if the runtime supports the UIPrintInteractionController - if it succeeds, you can use it, just make sure it's inside a conditional code block. Dec 8, 2010 at 18:58
  • Side Note: The "NSClassFromString" approach will NOT work in some cases - when the class exists but is different than the documented public version. For example, UITapGestureRecognizer exists in OS 3.1.3 but is not documented and public until 3.2; calling it on 3.1.3 crashes (accessing numberOfTapsRequired in particular).
    – Dad
    Feb 28, 2011 at 18:44
  • Ok. So you don't have to add the -weak_framework UIKit part to get this to work. However, "the compiler for your project must be the LLVM-GCC 4.2 compiler or newer, or the LLVM compiler (Clang) 1.5 or newer." as documented here: developer.apple.com/library/ios/#documentation/DeveloperTools/… AND note that it's 3.1 and later (no 3.0).
    – Dad
    Feb 28, 2011 at 19:10
2

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.

1
  • my issue with the 'block code' was, that i had LLVM compiler 1.6 set [it won’t weak-link support for blocks]... and it works perfectly fine with LLVM GCC 4.2. read marco.org/1648550153 (entry of Nov 22 2010) for more details.
    – jra
    Jan 29, 2011 at 10:38
1

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

1
  • i already have done that. the problem is, that the line of code with the completionHandler will cause the app not to run on iOS 3.1. but thanks to the answers i figured out why.
    – jra
    Nov 30, 2010 at 7:50
0
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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.