如何从 UIWebView 获取 URL 的哈希片段
我正在尝试获取 UIWebView 中加载的 URL 的哈希片段,我尝试了不同的方法,但它似乎不起作用。
例如,如果 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];
那么我想获取完整的网址,但它只返回“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"];
我错过了什么
I'm trying to get hash fragment of URL loaded in UIWebView, I have tried different approaches it does not seem to work.
For example if the UIWebView is loaded with "http://www.mysite.com/home#main":
NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/home#main"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
then I want to get the full url, but it only return "http://www.mysite.com/home" with 3 different approaches:
1:
NSString *currentURL = [webView.request.URL absoluteString];
2:
NSString *currentURL = [NSString stringWithFormat:@"%@",[[webView request] URL]];
3:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location.hash"];
what am I missing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
NSString *fragment = [webView.request.URL 片段];
Try
NSString *fragment = [webView.request.URL fragment];
[webview stringByEvaluatingJavaScriptFromString:@"window.location.hash"]
是我成功获取哈希片段的唯一方法,在我的情况下,我在方法 1 和方法 2 上失败了,
也许你的代码在其他方面是错误的......
[webview stringByEvaluatingJavaScriptFromString:@"window.location.hash"]
is the only way I had success to get the hash fragment in my caseI failed on approaches 1 and approaches 2
maybe your code wrong on someway else......
使用以下代码片段
NSLog(@"Hashfragment :%@",[NSString stringWithFormat:@"%@",webview.request.URL.fragment]);
Use the following code snippet
NSLog(@"Hash fragment :%@",[NSString stringWithFormat:@"%@",webview.request.URL.fragment]);
我认为你应该检查一下,request.url.absolutestring 在shouldStartLoadWithRequest、webViewDidFinishLoad 上有片段。
I think you should check that, request.url.absolutestring has fragment on shouldStartLoadWithRequest, webViewDidFinishLoad.