Loading local HTML in UIWebView

Discussion in 'iOS Programming' started by North Bronson, Mar 16, 2009.

  1. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #1
    If I have some local HTML content that I need to load in a UIWebView, it seems like there are two ways to do it:

    Code:
    NSString *fileString = [[NSBundle mainBundle] pathForResource: SomeLocalHTMLFile ofType: @"html"];
    
    NSString *newHTMLString = [[NSString alloc] initWithContentsOfFile: fileString encoding: NSASCIIStringEncoding error: NULL];
    
    NSURL *newURL = [[NSURL alloc] initFileURLWithPath: fileString];
    
    [myWebView loadHTMLString: newHTMLString baseURL: newURL];
    
    OR

    Code:
    NSString *fileString = [[NSBundle mainBundle] pathForResource: SomeLocalHTMLFile ofType: @"html"];
    
    NSURL *newURL = [[NSURL alloc] initFileURLWithPath: fileString];
    
    NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
    
    [myWebView loadRequest: newURLRequest];
    
    Is one of these ways preferred over the other? Both seem to work.

    On another note, if I do go with the first way, am I choosing the right String Encoding? I'm just loading a plain HTML file.
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    Basically, whichever one runs faster in a profiler. Failing that, whichever you find easier to read.

    Depends what encoding you wrote the file in. Assuming you're not doing anything special (foreign alphabets etc.), ASCII should be just fine.
     

Share This Page