1

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)

1
  • Read the comments in Rob's answer to see what worked for me. Commented Feb 28, 2014 at 10:39

2 Answers 2

1

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."}

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

11 Comments

Just to clarify are you saying that the ${APP_ID} is wrong or the use of localhost
@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.
@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).
...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
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.
|
0
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];

2 Comments

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.
would that be practical with internet hosted pages as well?

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.