I want to load a html page from a file, and append a hash tag to it. Is this possible?
I have tried
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"html"];
NSURL *fileUrl = [NSURL fileURLWithPath:[filePath stringByAppendingFormat:@"#hashtag"]];
[self.webView loadRequest:[NSURLRequest requestWithURL:fileUrl]];
NSLog(@"fileUrl = %@, reachable? %d", fileUrl, [fileUrl checkResourceIsReachableAndReturnError:nil]);
but this tries to look for the file someFile.html%23hashtag
, which can't be found. Is there a way to add the hash after the NSURL
object is created?
I've also tried loading the file into a string and using loadHTMLString
:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"html"];
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:fileContents baseURL:[NSURL URLWithString:@"http://someFile.html#hashtag"]];
Here the hash tag does work, but my javascript references inside the html don't work. A follow on question from this approach would be, how do I reference javascript files from html loaded in as a string in a UIWebView, ie, what is the base url?
A hack I can think of is to just put all my javascript files inline in the html and load it as a string, but I'm thinking there must be a better way!
As to %23 issue, I don't think Scott Kohlert's replace solution is good.
The following solutions seems better, I just copied it from here
A little bit off topic, I found the Safari on iOS 6.0, 6.1 behaves differenly than on iOS 5.1 and other desktop browsers(including Safari for OSX) regarding handle URL with anchor. For one of my particular document, whether in UIWebview or mobile Safari on iOS 6.0 or 6.1, upon the first load, the page isn't scrolled to the right position, a reload will fix it. Maybe it has something to do with the fact that part of the html is produced by javascript dynamically. Any ideas?