Navigating to local anchor link, even though your setting relative paths in a UIWebView
I've got a UIWebView that I wanted to set a baseURL for (it is a local to the iPhone html file, and has local image references). Unfortunately loadRequest doesn't allow you to set the baseURL (only loadHTMLString). I really don't know why.
So if you use loadHTMLString and pass it a blank string (and set the baseURL) and then call loadRequest, you can make it work.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath: path];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stores" ofType:@"html"];
NSURL *fullURL = [NSURL URLWithString:@"#OR" relativeToURL:fileURL];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
[webView loadHTMLString:@"" baseURL:baseURL];
[webView loadRequest:[NSURLRequest requestWithURL: fullURL]];
So if you use loadHTMLString and pass it a blank string (and set the baseURL) and then call loadRequest, you can make it work.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath: path];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stores" ofType:@"html"];
NSURL *fullURL = [NSURL URLWithString:@"#OR" relativeToURL:fileURL];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
[webView loadHTMLString:@"" baseURL:baseURL];
[webView loadRequest:[NSURLRequest requestWithURL: fullURL]];
Labels: anchor, baseURL, iphone, link, loadHTMLString, loadRequest, local, uiwebview

1 Comments:
You can use a <base href="..." /> tag (inserted by javascript injection if necessary). That'll work well until you need to use cookies at least.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home