0

I am building an app that allows the opening of a PDF, and handles this like so:

- (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;
}

And then in FilePreviewController.m:

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

    [self.documentInteractionController setDelegate:self];

    [self.documentInteractionController presentPreviewAnimated:NO];
}

However, when I open a file and the view transitions, I get these messages:

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

And the file itself does not load. I'm not sure what I'm doing wrong because this works fine on the simulator (i.e. the PDF loads and displays perfectly).

Someone
  • 428
  • 5
  • 17

1 Answers1

0

use this code to view/read pdf format file.

    .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];

                    }
                });
            });
Ananth
  • 26
  • 3