Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TurboLag

macrumors member
Original poster
Feb 24, 2004
85
0
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.
 

TurboLag

macrumors member
Original poster
Feb 24, 2004
85
0
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.
 

ace2600

macrumors member
Mar 16, 2008
71
0
Austin, Texas
Here's how I did it:
PHP:
NSString *urlPath = [URLUtil encodeURL:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"html"]]];
URL = [[NSURL alloc] initWithString: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;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.