Jesse Meyer
Jesse Meyer

Reputation: 315

NSURL is always nil dispite encoding

I'm generating a PDF file and am attempting to preview it as shown below, but URL routinely returns NIL despite my formatting (which is what seems to resolve everyone else's issue with this common problem). I must be missing something more. Any ideas?

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
NSString *docDirectory = [path objectAtIndex:0];

    docDirectory = [docDirectory stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *URL = [[NSBundle mainBundle] URLForResource:docDirectory withExtension:@"pdf"];
if (URL) {
    // Initialize Document Interaction Controller
    self->documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
    // Configure Document Interaction Controller
    [self->documentInteractionController setDelegate:self];
    // Preview PDF
    [self->documentInteractionController presentPreviewAnimated:YES];
}

Upvotes: 0

Views: 99

Answers (2)

rmaddy
rmaddy

Reputation: 318944

Your path creation seems all out of whack. Try something like this:

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
NSString *docDirectory = [path objectAtIndex:0];
NSString *filename = @"myfile.pdf"; // replace with the actual filename you used
NSString *fullPath = [docDirectory stringByAppendingPathComponent:filename];
NSURL *fullURL = [NSURL fileURLWithPath:fullPath];

In the code you posted you don't provide a filename. You have the Documents directory and the pdf extension. And there is no need to "percent escape" the URL in this case.

Upvotes: 1

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

You are probably confused of how to obtain the path to your file: NSSearchPathForDirectoriesInDomains is used to get an absolute path to (for example) your documents directory. NSBundle's URLForResource:withExtension: on the other hand searches the app wrapper for a file with the provided name.

Mixing the two would do no good. You should probably just look into the documents directory.

Upvotes: 0

Related Questions