View Full Version : Calling local html file in WebView
TurboLag
Jul 8, 2008, 02:57 PM
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:@"file://localhost/Users/turbolag/Desktop/Branches/Trunk/lvg.html"]]];
Thanks for any advice.
Sayer
Jul 8, 2008, 04:54 PM
NSString *tmpPath = [[NSBundle mainBundle] pathForResource:@"lvg" ofType:@"html"];
TurboLag
Jul 9, 2008, 10:18 AM
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
Jul 9, 2008, 10:04 PM
Here's how I did it:
NSString *urlPath = [URLUtil encodeURL:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"html"]]];
URL = [[NSURL alloc] initWithString:urlPath];
Here is URLUtil's encodeURL function
+ (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;
}
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.