I coded to print on the iPad and I've checked different documentation. My code seems to look right but when the controller is presented and I click on cancel it crashes with this error: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
I tried adding a dispatch_async but didn't help. This is my code:
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = self.title;
UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
pc.delegate = (id <UIPrintInteractionControllerDelegate>)self;
pc.printInfo = printInfo;
UIViewPrintFormatter *printFormatter = [self.webView viewPrintFormatter];
printFormatter.startPage = 0;
pc.printFormatter = printFormatter;
__weak SnaglistPrintViewController *weakSelf = self;
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed,
NSError *error) {
dispatch_async( dispatch_get_main_queue (), ^{
NSLog(@"Handler completed: %d", completed);
if (!completed && error) {
NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, (unsigned int)error.code);
}
else {
[weakSelf back];
}
} );
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pc presentFromRect:CGRectZero inView:self.view animated:YES completionHandler:completionHandler];
}