我想從一個文件中加載一個html頁面,併爲它添加一個散列標籤。這可能嗎?是否有可能發送一個哈希標籤到UIWebView中的文件url?
我已經試過
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]);
但這種嘗試查找文件someFile.html%23hashtag
,不能被發現。在NSURL
對象創建後有沒有添加散列的方法?
我也試着將文件加載到一個字符串,並使用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"]];
這裏的哈希標籤的工作,但我的HTML中的JavaScript引用不起作用。從這種方法的後續問題將是,我如何引用JavaScript文件作爲一個字符串在UIWebView中加載的HTML,即什麼是基本的網址?
我能想到的一個竅門就是把所有的javascript文件內嵌在html中,並將其作爲字符串加載,但我認爲必須有更好的方法!
這可以幫助ühttp://stackoverflow.com/questions/6691495/how-to-load-nsurl-which-contains -hash-fragment-with-uiwebview/43587401#43587401 – 2017-04-24 11:57:01