如何从UIWebView获取URL的哈希片段
我试图得到URL加载在UIWebView哈希片段,我已经尝试了不同的方法,它似乎并没有工作。
例如,如果UIWebView加载“http://www.mysite.com/home#main”:
NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/home#main"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request];
那么我想获得完整的url,但它只返回“http://www.mysite.com/home”3种不同的方法:
1:
NSString *currentURL = [webView.request.URL absoluteString];
2:
NSString *currentURL = [NSString stringWithFormat:@"%@",[[webView request] URL]];
3:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location.hash"];
我错过了什么
试试NSString *fragment = [webView.request.URL fragment];
[webview stringByEvaluatingJavaScriptFromString:@"window.location.hash"]
是我成功获得散列碎片的唯一方法
方法1失败,方法2失败
也许你的代码在别的地方错了……
使用下面的代码片段
NSLog(@“Hash fragment:%@”,[NSString stringWithFormat:@“%@”,webview.request.URL.fragment]);
我想你应该检查一下,request.url.absolutestring在shouldStartLoadWithRequest,webViewDidFinishLoad上有片段。