2

I'm new to Objective-C and iOS development. I can't get a local pdf file to display in UIWebView. I am setting up an array to hold the file names, then display them in the UIWebView. Any help would be appreciated!

From MasterViewController.m: (set array of pdf file names)

[mCommands addObject:[[NSDictionary alloc]
                    initWithObjectsAndKeys:@"Break",@"name",@"cmd_break.pdf",@"url",nil]];
[mCommands addObject:[[NSDictionary alloc]
                    initWithObjectsAndKeys:@"Close",@"name",@"cmd_close.pdf",@"url",nil]];

From DetailViewController.m: (display pdf file in UIWebView)

NSString *filename = [self.detailItem objectForKey:@"name"];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];
NSURL *detailURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:detailURL];
[self.detailWebView loadRequest:request];
3
  • I honestly can't believe how many people confuse Xcode, the IDE, with objective-c, the language. Commented Mar 4, 2012 at 14:13
  • 2
    Oh great one, perhaps you can reach down from the heights of the ivory tower you so obviously occupy and help out a poor ignorant beginner. Commented Mar 4, 2012 at 14:24
  • First, a couple of things. #1: Is your file Actually being copied to the application's bundle? In Xcode, go to Project->My Target->Build Phases->Copy Bundle Resources. If your PDF is not there, drag it from your project into that box. #2: Please Log 'path' before you load the WevView, It could be that the file name is wrong. Commented Mar 4, 2012 at 14:27

1 Answer 1

2

Your code to load the pdf into the view semse right.

But the filepath is probably wrong. When you use:

[[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];

It only needs the filename, it will append the type string. So you will probably get a result which ends with cmd_break.pdf.pdf

Verify this with:

NSString *filename = [self.detailItem objectForKey:@"url"];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];
NSLog(@"%@", path);

And yeah, as Richard J. Ross III pointed out. Your using the wrong key in your dictionary. The "name"-key will give you "Break" back while "url"-key will give you "cmd_break.pdf".

Sign up to request clarification or add additional context in comments.

3 Comments

Wait a minute, isn't the problem that he's loading the value for the 'name' key and not the 'url' key?
Correct. Missed that. Edited.
Thanks to both of you...that was the issue (using "name" instead of "url"). I also changed the array contents to not include the file extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.