2011-05-25 28 views
12

我想從一個文件中加載一個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中,並將其作爲字符串加載,但我認爲必須有更好的方法!

+0

這可以幫助ühttp://stackoverflow.com/questions/6691495/how-to-load-nsurl-which-contains -hash-fragment-with-uiwebview/43587401#43587401 – 2017-04-24 11:57:01

回答

23

我沒有試過這個,但如何加載文件通常沒有hashtag和實現UIWebViewDelegate這樣的事情?

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
    [webView stringByEvaluatingJavaScriptFromString:@"window.location.href = '#hashtag';"]; 
} 

參考文獻:

+0

我幾乎做到了這一點,但在html文件中。 – zlog 2011-09-05 10:53:48

+2

這對我有效('hash'而不是'href'):'[webView stringByEvaluatingJavaScriptFromString:@「window.location.hash ='#foo'」];' – canhazbits 2014-02-22 06:03:50

1

您應該能夠攔截NSURLRequest,其轉換爲NSMutableURLRequest,然後更改URL,如下所示。所有這些都會發生在shouldStartLoadWithRequest。確保你設置了UIWebView委託。

- (void) viewDidLoad 
{ 
    self.webView.delegate = self; 

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"html"]; 
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; 
} 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    //Make sure the request is mutable 
    if(![request isKindOfClass:[NSMutableURLRequest class]]) 
     [NSException raise:@"Need to change the request, but can't" format:@""]; 

    NSMutableURLRequest* mutableRequest = (NSMutableURLRequest*)request;  
    NSString* newUrl = [NSString stringWithFormat:@"%@#hashtag", [[request URL] absoluteString]]; 
    [mutableRequest setURL:[NSURL URLWithString:newUrl]];  

    return YES; 
} 

我還沒有遇到任何請求不可變的情況,但誰知道。

或者你可能要設置原始URL中的散列(如你在做),然後在shouldStartLoadWithRequest#取代%23第一次出現。

0

我認爲你必須創建文件的URL後

請問,如果你改變

NSURL *fileUrl = [NSURL fileURLWithPath:[filePath stringByAppendingFormat:@"#hashtag"]]; 

喜歡的東西它的工作添加哈希:

NSString *urlString = [[NSURL fileURLWithPath:filePath] absoluteString]; 
NSURL *fileUrl = [NSURL URLWithString:[urlString stringByAppendingString:@"#hashtag"]]; 
2

這是怎麼了我在我的代碼中執行。我將哈希標記附加到NSString,然後使用fileURLWithPath將它變成NSURL。然後,我將%23的所有實例都替換爲#。下面的代碼解釋了它的全部內容,但如果您有任何問題,請告訴我們。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"html"];  
    NSString *filePathWithHash = [NSString stringWithFormat:@"%@#yourDesiredHashTagHere",filePath]; 
    NSURL *theURL = [NSURL fileURLWithPath:filePathWithHash]; 
    //NSURL turns the # into %23 when using fileURLWIthPath. So we need to change it back: 
    NSString *finalURLString = [[NSString stringWithFormat:@"%@",theURL] stringByReplacingOccurrencesOfString:@"%23" withString:@"#"]; 
    //Then we need to change it back to an NSURL 
    NSURL *finalURL = [NSURL URLWithString:finalURLString]; 
    //And finally we load this in the webView 
    [theWebView loadRequest:[NSURLRequest requestWithURL:finalURL]]; 
+0

你也可以跳過替換步驟: 'NSURL * url = [NSURL URLWithString:[[[NSURL fileURLWithPath:filePath] .absoluteString stringByAppendingString:@「#」] stringByAppendingString:@「yourDesiredHashTagHere」]];' – 2016-05-27 05:44:34

10

至於%23問題,我不認爲Scott Kohlert的替換解決方案是好的。

下面的解決方案似乎是更好的,我剛從here

NSURL *baseUrl = [NSURL fileURLWithPath:stringUrl]; 
NSURL *fullURL = [NSURL URLWithString:@"#jumpto" relativeToURL:baseUrl]; 

一點點題外話複製它,我發現了Safari瀏覽器在iOS 6.0,6.1的行爲differenly比在iOS 5.1和其他桌面瀏覽器(包括Safari的OSX)關於處理URL與錨點。對於我的特定文檔之一,無論是在iOS 6.0還是6.1的UIWebview或移動Safari中,在第一次加載時,頁面都不會滾動到正確的位置,重新加載將修復它。也許這與html的一部分是由javascript動態生成的事實有關。有任何想法嗎?

+3

我相信這比在評估結束加載視圖的JavaScript更好。目前選擇的答案是哪一個。這應該是正確的答案。 – h3dkandi 2016-06-29 13:42:29

1

除了@ xiang的回答。下面的快捷版本,如果你在你的應用程序的本地HTML文件的工作:

let urlString = "relative/path/to/your/index.html" 
let anchorString = "#your-anchor" 
let baseURL:NSURL? = NSBundle.mainBundle().URLForResource(urlString, withExtension: nil, subdirectory: nil) 
if let url:NSURL? = NSURL(string: anchorString, relativeToURL: baseURL) { 
    // Do something with your URL 
} 
相關問題