2015-07-20 65 views
0

加載文件我建立一個應用程序,允許PDF的開放,並處理這個像這樣:不能在UIDocumentInteractionController

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(NSString *)source annotation:(id)annotation 
{ 
    if (url != nil && [url isFileURL]) 
    { 
     if ([[url pathExtension] isEqualToString:@"pdf"]) 
     { 
      FilePreviewViewController *filePreviewController = [[FilePreviewViewController alloc] init]; 
      [filePreviewController setURL:url]; 

      [self.navController pushViewController:filePreviewController animated:NO]; 

      return YES; 
     } 
    } 

    return NO; 
} 

然後在FilePreviewController.m

-(void) setURL:(NSURL *) URL 
{ 
    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL]; 

    [self.documentInteractionController setDelegate:self]; 

    [self.documentInteractionController presentPreviewAnimated:NO]; 
} 

然而,當我打開文件和視圖轉換,我得到這些消息:

Unbalanced calls to begin/end appearance transitions for <QLRemotePreviewContentController: 0x1581fe00>. 

Couldn't issue file extension for path: /private/var/mobile/Containers/Data/Application/6399D00B-47A1-4F33-B34C-3F0B07B648AE/Documents/Inbox/pdf-8.pdf 

而文件本身沒有噸負載。我不確定我在做什麼錯誤,因爲這在模擬器上工作正常(即PDF加載和完美顯示)。

回答

0

使用此代碼查看/閱讀PDF格式的文件。

.h 
    <UIDocumentInteractionControllerDelegate> 

    UIDocumentInteractionController *controller; 

.m 

NSString *File_name=[NSString stringWithFormat:@"%@",URL]; 
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
      NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@",docDir,[File_name lastPathComponent]]; 

      NSData *pdfData = [[NSData alloc] initWithContentsOfURL:URL]; 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

       dispatch_async(dispatch_get_main_queue(), ^{ 

        if (pdfData) { 

         [pdfData writeToFile:pngFilePath atomically:YES]; 


         controller = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:pngFilePath]]; 
         controller.delegate = self; 
         CGRect rect = CGRectMake(0, 0, appDelegate.wVal, appDelegate.hVal); 
         [controller presentOptionsMenuFromRect:rect inView:self.view animated:YES]; 

        } 
       }); 
      }); 
相關問題