Calling local html file in WebView

Discussion in 'Mac Programming' started by TurboLag, Jul 8, 2008.

  1. TurboLag macrumors member

    Joined:
    Feb 24, 2004
    #1
    I am using a WebView to store a locally saved web page, which is in the /Resources of my XCode project. I can point to the file with an absolute pointer, but I want to point to the file relatively so it will work on any system. Below is how the file is being loaded:

    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:mad:"file://localhost/Users/turbolag/Desktop/Branches/Trunk/lvg.html"]]];

    Thanks for any advice.
     
  2. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #2
    NSString *tmpPath = [[NSBundle mainBundle] pathForResource:mad:"lvg" ofType:mad:"html"];
     
  3. TurboLag thread starter macrumors member

    Joined:
    Feb 24, 2004
    #3
    Thanks, but no dice.

    I have added the following line:
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:tmpPath]]];

    I think it has to do with the space character in my path, which is not URL friendly. Is there a method which would accept an NSString and return another with ' ' replaced by '%20'? Or should I change my target name?

    Thanks.
     
  4. ace2600 macrumors member

    Joined:
    Mar 16, 2008
    Location:
    Austin, Texas
    #4
    Here's how I did it:
    PHP:
    NSString *urlPath = [URLUtil encodeURL:[[NSBundle mainBundlepathForResource:@"fileName" ofType:@"html"]]];
    URL = [[NSURL allocinitWithString:urlPath];
    Here is URLUtil's encodeURL function
    PHP:
    + (NSString *)encodeURL:(NSString *)URL {
        
    //Here is the conversion of ASCII to UTF8. This is what you're looking for.
        
    NSMutableString *mutableURL = [NSMutableString stringWithString:[URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        
    //This was a custom change, you probably won't need it
        
    [mutableURL replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableURL length])];
        
    //Another custom change, you probably won't need it
        
    [mutableURL replaceOccurrencesOfString:@"'" withString:@"%22" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableURL length])];
        return 
    mutableURL;
    }
     

Share This Page