I already tried getting the current URL of my UIWebView with: webview.request.URL.
Unfortunately the NSURL was empty. Anything wrong here? I am working with Xcode 3.2.2 beta 5.
The code above should be executed in the UIWebView delegate didStartLoad.
window.location via JS didn't work reliably for me, but this did:
currentURL = currentWebView.request.URL.absoluteString;
Credit:
http://mohrt.blogspot.com/2008/10/getting-url-from-uiwebview.html
here's the code I use to grab the url every time you navigate to a different link within the webview:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
self.url = aWebView.request.mainDocumentURL;
}
Matt's version is much cleaner. I recommend everyone to use that one instead of this
You could try this:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:#"window.location"];
I too found that the approved answer above was not reliable.
But with a slight modification, it seems to work every time:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
Note the addition of ".href" to the Javascript as this is hidden at the end of the line of code.
This is not correct, and will return a nil:
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:#"window.location"];
However, the code below can get a URL, but the url may not be the current URL:
NSString *url = _webView.request.URL.absoluteString;
The correct one is:
NSString *currentURL = [_webView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSURL *currentURL = [[webView request] URL];
NSLog(#"%#",[currentURL description]);
}
Tried this for google search results on iPhone:
NSString* currentURL = webView.request.URL.absoluteString;
NSString* mainDocumentURL = webView.request.mainDocumentURL.absoluteString;
Both return the same string!
As UIWebView is deprecated, for WKWebview to get the current url is very simple.
webView.url
here the code i use :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[[webView request] URL] absoluteString]]];
I use the shouldStartLoadWithRequest event (UIWebViewDelegate) to catch URL updates. request.mainDocumentURL.absoluteString will get you the main web page's URL (which is normally what you want), while request.URL.absoluteString will include CSS and JavaScript includes.
This always works . .
NSString* url= [webView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
implement delegate method,
- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *URL = request.URL.absoluteString; NSLog(#"%#",URL);
}
URL is the what you exactly needed.
IN Swift try this,
func webViewDidFinishLoad(webView: UIWebView){
println(WebView.request?.mainDocumentURL)
}
To get current URL of the WKWebView and UIWebview
Here is the code.
if (self.wkWebView) {
NSString *URL = self.wkWebView.title;
}else if(self.uiWebView) {
NSString *URL = self.uiWebView.request.title;
}
Related
NSString *testString =[NSString stringWithFormat:#" Google"];
[webView loadHTMLString:testString baseURL:nil];
I want to get the URL when I click on this UIWebView content in
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:inRequest.mainDocumentURL];
return NO;
}
return YES;
}
delegate method.
When I click google hyperlink in webview it gives
URL: applewebdata://
Please help me.
You question is not very clear if you want to get the tapped url on webview or the webview's loaded page url ,
If you want to get webview loaded page url you can use NSURLRequestObject,something like
NSString * url = [request URL];
If you want to get the clicked link url , you will have to use this java script
NSString *url = [webView stringByEvaluatingJavaScriptFromString:#"window.location"];
you can do like this way
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.absoluteString rangeOfString:#"www.google.com"].location!=NSNotFound)
{
[[UIApplication sharedApplication]openURL:request.URL];
return NO;
}
return YES;
}
For Swift:
Whenever your webpage is loaded you can use this, I use it for a label in this case:
webUrlLabel.text = webView.request.URL.absoluteString
For Objective-C:
NSString *currentPage = [NSString stringWithFormat:#"%#", webView.request.URL.absoluteString];
webUrlLabel is a UILabel
webView is my UIWebView
You already have NSURLRequest object. Access url from that.
inRequest.URL
way 1:
NSString *currentURL = myWebView.request.URL.absoluteString;
Way 2:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
yourTextBox.text = [url absoluteString];
return YES;
}
Please let me know if it works. Thanks
How to make this statement into an actual "if statement" in Xcode?
This is what i want in the form of an "if statement" :
"if a certain link is shown in UIWebView, then image.hidden = NO;"
The UIWebView is currently showing another website, but it has a page in that website that is the certain link I mentioned in the if statement.
The code for the UIWebView is this:
.h:
IBOutlet UIWebView *Name;
NSURL *NameURL;
.m
NameURL = [NSURL URLWithString:#"mywebname.com"];
NSURLRequest *Request = [NSURLRequest requestWithURL: NameURL];
[Name loadRequest: Request];
To access the actual page content within UIWebView, you need to use
stringByEvaluatingJavaScriptFromString: to invoke some JavaScript snippet. Once the page finishes loading, the idea is to loop through all a elements and check for its href attribute, comparing it to the 'certain site' that you mentioned.
Rough implementation:
JS Code
for (var i = 0; i < document.getElementsByTagName('a').length; i++)
{
if (document.getElementsByTagName('a')[i].href.match(/google/) return 'YES';
}
return 'NO';
Obj-C Code
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *test = [webView stringByEvaluatingJavaScriptFromString:aboveJSString];
if ([test isEqualToString:#"YES"]) image.hidden = NO;
}
Hope it helps.
According to Anh Do's answer stringByEvaluatingJavaScriptFromString isn't reliable for me.
Instead you should use:
currentURL = currentWebView.request.URL.absoluteString;
So the code will be like….
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"HELLO DID GET CALLELDLLL LL LL LL ");
//get the url you webView called "Name" is displaying...
NSString *currentURL = Name.request.URL.absoluteString;
//Check if that url is "mywebname.com"
if ([currentURL isEqualToString:#"mywebname.com"])
{
image.hidden = NO;
}
}
PS. Don't for get to add delegate to you webview
By setting delegate do this..
In ViewDidLoad:
[Name setDelegate:self];
In .h file
#interface "nameofyourviewcontroller" : UIViewController<UIWebViewDelegate>
I´ve a small app with a UIWebView for web surfing in it. On some pages opens the Appstore for promotional purposes (that is annoying). How can i prevent that? Is there a special method? or just fake the browserid?
http://bjango.com/articles/ituneslinks/
here is the complete reference for link formation of the appstore, itunes.
from above reference link, apple.com is common for all kind of links.
So we can create regex or simply search string "apple.com" from url and avoid to load in webview.
If you wanna,use without regex following code may be help you :
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *currentURL = request.URL;
NSString *urlString = url.absoluteString;
NSRange range = [urlString rangeOfString:#"apple.com"];
if (range.location != NSNotFound)
return YES;
else
return NO;
}
Use UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType: to detect what URLs are getting loaded in the webview. App store URLs usually contain itunes.apple.com or phobos.apple.com.
When you encounter such urls are clicked, you can return NO from the web view delegate method to stop loading the url.
Hope that helps!
Use the following UIWebView Delegate method for this :
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *currentURL = request.URL;
NSString *urlString = url.absoluteString;
NSRange range = [urlString rangeOfString:#"https://itunes.apple.com"]; // Change URL if other than this.
if (range.location != NSNotFound)
return YES;
else
return NO;
}
I have NSString as follows
NSString *textOutStations = [NSString stringWithFormat:#"Hello Everyone. Please check out website:<br> http://www.google.com/</br>"];
I want to show google.com as below in URL, as I will load this in UIWebView.
www.google.com
So, whenever user click it, It must open Safari in iPhone.
NSString *textOutStations = [NSString stringWithFormat:#"Hello Everyone. Please check out website:<br> http://www.google.com/"];
[self.webView loadHTMLString:textOutStations baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
Then in UIWebView delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// Opening safari
[[UIApplication sharedApplication] openURL:request.URL];
....
}
Try this one
NSString *textOutStations = #"<html><head><title></title></head><body><div> Hello Everyone. Please check out website:<br/> http://www.google.com/ </div></body></html>";
[self.webView loadHTMLString:textOutStations baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
This will help you......
You can not specify a URL with in a string and have it be clickable, this type of functionality is dependant on the object to which is using it, aka, UITextView UITextField UILabel UIWebView etc,
A UIWebview will show your url with in the webview it will not open the link in safari. IF you want to load it in ui web view, it's already ansered above, if you want to open it in safari, you have to do
[[UIAplication sharedApplication] openUrl:urlObject];
if you want to open it to be text with in a UITextView i would suggest this other stack overflow link here
I checked the link option in Attribute Inspector of UIWebView and it detected the link.
Method to OPEN in SAFARI...
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
I want to update URL in a textfield in Xcode using WKWebkit when any website is selected. In case of WebView we do like this :-
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
NSURL *requestURL = [webView.request URL];
_txtAddress.text = [requestURL absoluteString];
}
But how to do in WkWebkit i don't know ?
WKWebKit has its own navigationDelegate and you can implement that
protocol to do the same things
here is the one for the didFinishNavigation which you can use.