How can I receive data(url) from webpage loaded in uiwebview? - iphone

I've UIWebView with webpage loaded. This webpage is simple - one picture which follows somewhere
How can I receive this URL after taping for handling in xcode? I don't want to open this link in the same web view(what is happened now). Thanks for any tip.

Use this delegate function.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSURL *regURL=[request URL];
NSString *urlString=[regURL absoluteString];
}

Related

ios webview how to monitor the webview load a new url

**Situation is like this,my webview load a base request url,when user clicked the html or anyother action,the webview will laod a new url. Can I get the webview changed events? If window.location.href can be worked well,and how?
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
this method will be performed every time you click the button of html

Open webView link in Safari from Javascript?

Thanks for looking at my question. To do some testing, I've output the string for the current URL of a webView into the console. But with m.youtube.com, it seems that no URL is passed. This is probably the reason why using methods like:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
don't work with m.youtube.com, but do with other websites.
As Mathew said, YouTube loads using Javascript, so let me rephrase my question - is there a way to open the site in Safari when new javascript is loaded on the page?
I've uploaded a sample project that contains my problem.
https://www.dropbox.com/sh/m4yjv7awpj9wlh8/q0nQgHlQQC/YouTubeWebView
Thanks very much!
I think you can refer this,may be this can helpful to you in passing your webview request in safari without URL

webViewDidStartLoad Request URL Not Set to URL Being Loaded

I am working on a browser that has back and forward buttons, that when tapped, should immediately display the URL of the page being loaded. Currently I do something like [webView goBack]; and then implement the delegate UIWebView method:
- (void)webViewDidStartLoad:(UIWebView *)aWebView {
NSLog(#"began loading URL: %#", aWebView.request.URL);
}
But this doesn't result in the behavior I expect.
For example, if I start out at www.google.com and then navigate to www.aol.com and then press the back button causing the web view's goBack code to be called, the NSLog will output began loading URL: http://www.aol.com/ even though it should state that google.com is being loaded. The google page will load perfectly however in the web view. What I don't understand is why the URL isn't consistent with what is being loaded. On occasion the log will spit out that aol.com is being loaded, followed by google.com, but this only happens sometimes. Is this a bug with the SDK or am I missing something?
Thanks in advance.
try in this delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"Request Url:%d",request.URL.absoluteURL);
}
I suggest to use an array of string every time you load a new url you add it in the array and beside it define a global counter increase/deacrease based on back/forward click so you can get the url easy like this
NSString *urlString = [urlArray objectAtIndex:theCounterValue];
try:
NSString *currentURL;
currentURL = [[NSString alloc] initWithFormat:#"%#", webView.request.mainDocumentURL];

Call action from UIWebView with JavaScript

I am trying to build a simple app that uploads images to my site. I am using this tutorial:
http://www.youtube.com/watch?v=aQXaJO36I7Y
What I want to do instead of using a button from the app's interface I wan to use a button within a website in a webview. So I want a button to call the opening of the photo album to select a image to upload.
Basically I need a way for a javascript function to call a action in the app itself.
How can I do this?
You could just use a custom link, which you can then detect in the UIWebViewDelegate method.
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *rawURL = [[request URL] absoluteString];
if([rawURL isEqualToString:#"callImageLib://"]) {
[self methodeForImageLib];
}
return NO;
}
return YES;
}
If i understand it correctly you need to communicate iPHone Web application to call native iPhone application and visa versa. please check the following links.
http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript
http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview
hope these 2 link help to solve your issue.

Prevent app exit from UIWebView link

I have a UIWebView in my app which shows the clients webpage, however I would like to retain the user within the app if they choose to browse the webpage.
Can I prevent it breaking out to Safari when a link is clicked?
Try implementing UIWebViewDelegate's:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
HTH.