Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When using a WebView to load local HTML files I am able to intercept the requests for content associated with the HTML website.

web_view.Delegate = new ViewDelegate();

If I define a new class with four overloaded methods I can intercept these requests.

public class ViewDelegate : UIWebViewDelegate
{
    private string new_req;
    private NSUrlRequest m_request;

    public override bool ShouldStartLoad (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
    {
        new_req = request.Url.ToString ().Substring (request.Url.ToString ().LastIndexOf ('/') + 1);

        if (new_req != "webloader.html") 
        {
            HandlerBase handle = Registrar.Instance.GetHandler ("Filehandler");
            string filePath = handle.DoHandle (new_req);

            if (File.Exists (filePath)) 
            {
                m_request = new NSUrlRequest (new NSUrl (filePath, false));
            } else {
                Console.WriteLine ("Error! File not found: " + new_req);
            }
        }
        return true;
    }

    public override void LoadingFinished (UIWebView webView)
    {

    }

    public override void LoadFailed (UIWebView webView, NSError error)
    {

    }

    public override void LoadStarted (UIWebView webView)
    {
        if (m_request != null)
            webView.LoadRequest (m_request);
        else
            Console.WriteLine ("Request not handled by DB.");
    }
}

The first HTML file loaded is always "webloader.html". This file contains an iframe tag which embeds another HTML file within itself by reference.

<!DOCTYPE html>
<style>
*{
    width:100%;
    height:100%;
    overflow:hidden;
    background-color: black;
}
</style>
<html><body>
    <iframe frameborder="1"  src="index.html"></iframe>
</body></html>

What happens first is that the ShouldStartLoad method is invoked and the webloader.html file is requested. Since this file resides in the Content folder, I return true and let the WebView find it automatically. Next request is for index.html. Now I use my DoHandle method to fetch the webpage from the database and write it to memory and return the path of the file (I have also tried returning the content as a byte[] and load it directly, but this does not solve my problem). The path is then wrapped in a NSUrlRequest object and loaded using the webView.LoadRequest method within the LoadStarted method.

Sidenote: The index.html is generated by Maqetta and contains several paths referencing libraries like Dojo. The WebView needs to locate and load all of these files in order to display properly.

My problem is that the next request fired by the WebView is always the same as the one it just loaded from the DB. This causes an infinite loop and hard crash.

This approach works just wonderfully on MonoDroid which always asks for the NEXT request after receiving one using the WebView.LoadData method. Why isn“t the UIWebView asking for more HTML dependencies?? If i break the requests by returning false in the ShouldStartLoad method, after receiving a request two times in a row, all i see is a non-formatted HTML page without its libraries needed to load themes etc.

Is there anyone out there who has had experience in loading local content to a UIWebView from a SQLite database? You should be sitting on the solution to my question.

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.