Ios UIPrintInteractionController canPrintUrl always false,错误为CFURLCreateDataAndPropertiesFromResource:失败,错误代码为-15

Ios UIPrintInteractionController canPrintUrl always false,错误为CFURLCreateDataAndPropertiesFromResource:失败,错误代码为-15,ios,objective-c,iphone,Ios,Objective C,Iphone,我面临UIPrintInteractionController canPrintUrl始终返回false和错误代码的问题 :CFURLCreateDataAndPropertiesFromResource:失败,错误代码为-15。 :ImageIO:CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource失败,错误代码为-15 打印url的说明: /var/mobile/Applications/49C03664-61

我面临UIPrintInteractionController canPrintUrl始终返回false和错误代码的问题 :CFURLCreateDataAndPropertiesFromResource:失败,错误代码为-15。 :ImageIO:CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource失败,错误代码为-15

打印url的说明: /var/mobile/Applications/49C03664-6108-40D2-94A3-C545F0234ACE/Documents/mypdf.pdf

它是一个有效的PDF文件,在adobe reader和UIWebView中打开

-(void)printButtonTapped:(CGRect)rect
{
    if ([UIPrintInteractionController isPrintingAvailable] == YES)
    {
        // NSURL *fileURL = document.fileURL; // Document file URL
        NSString* str=[NSString stringWithFormat:@"%@/Documents/mypdf.pdf",NSHomeDirectory()];
        NSURL* url=[NSURL URLWithString:str];



        if ([UIPrintInteractionController canPrintURL:url] == YES)
        {
            self.printInteraction = [UIPrintInteractionController sharedPrintController];

            UIPrintInfo *printInfo = [UIPrintInfo printInfo];
            printInfo.duplex = UIPrintInfoDuplexLongEdge;
            printInfo.outputType = UIPrintInfoOutputGeneral;
            printInfo.jobName = [url lastPathComponent];;

            self.printInteraction.printInfo = printInfo;
            self.printInteraction.printingItem = url;
            self.printInteraction.showsPageRange = YES;

            if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad) // Large device printing
            {
                [self.printInteraction presentFromRect:rect inView:self.view animated:YES completionHandler:
                 ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
                 {
#ifdef DEBUG
                     if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
#endif
                 }
                 ];
            }
            else // Handle printing on small device
            {
                [self.printInteraction presentAnimated:YES completionHandler:
                 ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
                 {
#ifdef DEBUG
                     if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
#endif
                 }
                 ];
            }
        }
    }
}
我在WevView中加载上面的url,然后它显示PDF。 如果我将pdf加载的webview.request.url传递给[UIPrintInteractionController canPrintURL:webview.request.url] 然后工作良好并打印pdf

我不知道出了什么问题,我正在运行ios7和ios8


有人能帮我解决这个问题吗?

在指定位置找不到文件,或者文件名包含空白。webview在文件URL中需要编码的空格,而UIPrintInteractionController在文件URL中需要普通空格。解决此问题。

您必须了解canPrintData和canPrintURL之间的区别:

代码

这是不对的。对于本地文件,您应使用:

NSString* str=[NSString stringWithFormat:@"%@/Documents/mypdf.pdf",NSHomeDirectory()];
NSData *data = [NSData dataWithContentsOfFile:str];
if ([UIPrintInteractionController canPrintData:yourfilepath] == YES)

使用UIPrintInteractionController.printableUTIs检查文件类型.pdf、.docx、.txt是否支持打印

我也有同样的问题。您找到解决方案了吗?如果[UIPrintInteractionController isPrintingAvailable]==是,并且在iOS 8目录中,每次运行时路径都会更改,那么我将删除该选项,以便确认您指向的是有效的现有文件。
NSString* str=[NSString stringWithFormat:@"%@/Documents/mypdf.pdf",NSHomeDirectory()];
NSData *data = [NSData dataWithContentsOfFile:str];
if ([UIPrintInteractionController canPrintData:yourfilepath] == YES)