Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am attempting to load a locally hosted html file into a webview from an array. it looks something like this

_siteAddresses = [[NSArray alloc]
                      initWithObjects:@"file://localhost/var/mobile/Application/${APP_ID}/First Pentecostal Seminary.app/First_Pentecostal_Seminary/Main.html",...

with the corresponding code being

NSString *urlString = [_siteAddresses
                       objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

self.detailViewController.webView.scalesPageToFit = YES;

[self.detailViewController.webView loadRequest:request];

What am I doing wrong...is it my coding or perhaps the html coding (I believe there may be some java in there)

share|improve this question
    
@OmnicrinCS is your issue fixed – Charan Giri Feb 27 '14 at 5:15
    
Read the comments in Rob's answer to see what worked for me. – OmnicronCS Feb 28 '14 at 10:39
up vote 1 down vote accepted

Two thoughts:

  1. That example file URL doesn't look right. How did you construct that? If it was something from your bundle on your device, I'd expect something more like:

    file:///var/mobile/Applications/9E670C3C-C8B1-4B09-AE66-B43F7DB29F4D/First Pentecostal Seminary.app/...
    

    Obviously, you have to programmatically determine this URL by using NSBundle instance method URLForResource or by using the bundle's bundleURL and then adding the appropriate path components.

  2. You should (a) specify the view controller to be the delegate for your web view; and then (b) implement webView:didFailLoadWithError: and look at the error there, and it will inform you what the error was, if any.


For example, I have a file, test.html sitting in my bundle, which I can load into a web view like so:

NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSURL *url = [bundleURL URLByAppendingPathComponent:@"test.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

I have set up my view controller as the delegate for the web view and have the following didFailLoadWithError:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"%s: %@", __FUNCTION__, error);
}

So, when I tried to load a request with an invalid URL this time (test2.html, which I do not have in my bundle), I get the following error:

2014-02-26 23:35:43.593 MyApp[3531:70b] -[ViewController webView:didFailLoadWithError:]: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x8c32f40 {NSErrorFailingURLStringKey=file:///Users/user/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF/MyApp.app/test2.html, NSErrorFailingURLKey=file:///Users/user/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF/MyApp.app/test2.html, NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x10424c90 "The requested URL was not found on this server."}

share|improve this answer
    
Just to clarify are you saying that the ${APP_ID} is wrong or the use of localhost – OmnicronCS Feb 27 '14 at 3:57
    
@OmnicronCS Personally, I'm suspect about both. The file:// URL I provided as an example is a valid one I got from URLForResource. Generally, I'd just have string literals with the relative local path within the bundle, and then use those NSBundle methods I reference above to determine the full URL at runtime. – Rob Feb 27 '14 at 4:00
    
@OmnicronCS By the way, I'd also manually inspect the bundle (e.g. find the bundle sitting in your Xcode project's Products folder; choose "Show in Finder" and once looking at it in Finder, choose "Show Package Contents") and make sure (a) the files are there at all (because it's too easy to add files to a project and accidentally neglect to include them in the bundle); and (b) the files are where you think they are (because folders in Xcode don't always translate to subdirectories in the bundle; it depends upon how the items were added to the project). – Rob Feb 27 '14 at 4:07
    
...The code was a compilation of two answers from similar questions. How should I implement the webView:didFailLoadWithError:...the only examples I have seen indicate no internet connection – OmnicronCS Feb 27 '14 at 4:17
    
Oddly enough I cannot select "Show in Finder" from the product. I did check my build phases and saw that the files are listed as resources. – OmnicronCS Feb 27 '14 at 4:30
Try this code   

UIWebView *documentsWebView=[[UIWebView alloc]init];
documentsWebView.delegate=self;
documentsWebView.frame=CGRectMake(0, 0, 1024, 616);  
documentsWebView.backgroundColor=[UIColor clearColor];  
[self.view addSubview:documentsWebView];    
NSString* htmlString = [NSString stringWithContentsOfFile:[_siteAddresses
                   objectAtIndex:indexPath.row] encoding:NSUTF8StringEncoding error:nil];  
[documentsWebView loadHTMLString:htmlString baseURL:nil];
share|improve this answer
    
I like your approach my only problem with this is that this is that I am loading both local and internet hosted webpages from an array. – OmnicronCS Feb 27 '14 at 12:30
    
would that be practical with internet hosted pages as well? – OmnicronCS Feb 27 '14 at 23:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.