Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-17-2008, 03:59 PM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 4
jdegrace is on a distinguished road
Default Display a local file in a UIWebView

Is is possible to display a local file from the mainBundle in a UIWebView? For example, a PDF file.

Thanks!
jdegrace is offline   Reply With Quote
Old 08-17-2008, 06:10 PM   #2 (permalink)
Lost in a sea of code
 
BostonMerlin's Avatar
 
Join Date: Apr 2008
Location: Boston
Posts: 399
BostonMerlin is an unknown quantity at this point
Default

I don't see why not as long as it's either a resource in your app, a url to the pdf or something you streamed from the web and downloaded to your apps directory... just point the UIWebView to the file.. should load up just fine.

john
__________________
----------------------------------------------------------------------
I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
BostonMerlin is offline   Reply With Quote
Old 08-17-2008, 09:28 PM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 4
jdegrace is on a distinguished road
Default Display a local file in a UIWebView

I thought so, too. I tried the following, but I think I'm probably doing something wrong because it doesn't display anything.

- (void)viewDidLoad {

NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"EX" ofType:@"pdf"];
NSURL *url = [NSURL URLWithStringdfPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:urlRequest];

}
jdegrace is offline   Reply With Quote
Old 08-17-2008, 09:42 PM   #4 (permalink)
I can't think without pen
 
Join Date: Jul 2008
Location: New York
Posts: 72
aahmed753 is on a distinguished road
Default

I think that UIWebView is very trimmed version of the Safari. It doesn't even recognize or can't handle new window links... or javascript open window script
aahmed753 is offline   Reply With Quote
Old 02-11-2009, 02:47 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 176
Soleil is on a distinguished road
Default

I have the same problem - nothing is displayed. What's wrong with this code?

Code:
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];

NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
If I replace urlAddress with this, it works:
//NSString *urlAddress = @"http://www.google.com";
Soleil is offline   Reply With Quote
Old 02-11-2009, 03:40 PM   #6 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
RickMaddy will become famous soon enough
Default

To use a local resource file in a web view you need to use NSURL fileURLWithPath, not NSURL URLWithString.

Code:
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];

NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
RickMaddy is offline   Reply With Quote
Old 02-11-2009, 03:59 PM   #7 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
lbendlin is on a distinguished road
Default

it also helps to set the BaseURL if you want to embed local images into the UIWebView.

Code:
#define BASEURL [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]
lbendlin is offline   Reply With Quote
Old 02-11-2009, 04:45 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 176
Soleil is on a distinguished road
Default

thank you, that worked.
Soleil is offline   Reply With Quote
Old 12-11-2009, 04:18 PM   #9 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Atlanta
Posts: 411
funkytaco is on a distinguished road
Default

What's a simple way to determine if a file is local or remote?

I'm saving the request.URL from webView:webView shouldStartLoadWithRequest: navigationType:

and I don't want it to display a URL if it's a local file.
funkytaco is offline   Reply With Quote
Old 04-18-2010, 11:40 AM   #10 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 159
MiniRobinho is on a distinguished road
Default

I just tested this out, and the zooming is extremely slow - it jams for 10 seconds everytime you zoom until the text and images are in focus. Very frustrating. Is there any better way to display a PDF?
MiniRobinho is offline   Reply With Quote
Old 11-11-2010, 04:05 PM   #11 (permalink)
yoz
Registered Member
 
Join Date: Jul 2010
Posts: 16
yoz is on a distinguished road
Default

I used the code from RickMaddy, put it in the viewDidLoad function, and linked it up to an -(IBAction).

When the 4 page 700KB PDF opens (encoded by Adobe Acrobat 8.1 Paper Capture Plug-in), or any other PDF I have in my bundle, it opens with blank pages and a black column on the right.

Is this an encoding problem or have I done something wrong?
yoz is offline   Reply With Quote
Old 01-08-2011, 04:23 PM   #12 (permalink)
Registered Member
 
Join Date: Oct 2010
Location: PA
Posts: 11
RyanG is on a distinguished road
Default

I have this same problem. I have a PDF file I want to load, I have tried in the WebView but I could never get it to work; but as you guys stated it loads very slow. I have also tried to save the PDF as a PNG and display it that way, no go.

I can load it just from the website the PDF is located, but I want it to be available for users offline.

Has anyone found a successful solution to this problem? I'd greatly appreciate it!!
RyanG is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 314
13 members and 301 guests
chiataytuday, coolman, givensur, glenn_sayers, guusleijsten, ipodphone, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:25 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0