Ios – How to get hash fragment of URL from UIWebView

fragmenthashiosnsurluiwebview

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

Best Answer

Try NSString *fragment = [webView.request.URL fragment];