How to load NSURL which contains hash fragment “#” with UIWebView?

前端 未结 4 1824
刺人心
刺人心 2021-01-11 20:16

Given a local URL address like index.html

Now I need to use UIWebView to load it in iPad. I followed these steps:

  1. Create NSURL

    <
4条回答
  •  时光说笑
    2021-01-11 21:01

    For swift 3 solution1: You can call this line after webview loaded

    webView.stringByEvaluatingJavaScript(fromString: "window.location.href = '#myHashtag'")
    

    solution2: also if you want to load directly use this

    webView1.loadRequest(URLRequest(url: URL(string: url! + "#myHashtag")!))
    

    ObjC: solution 1:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
    

    [webView stringByEvaluatingJavaScriptFromString:@"window.location.href = '#myHashtag';"]; }

    solution2:

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat: @"%@#myHashtag",myURL]]]];
    

提交回复
热议问题