iOS get destination url before uiwebview goBack - iphone

I set a custom button that executes a goBack method of uiwebview. However, I need then to get destination url when this button is pressed. How could I acces to first position of navigation history? Thank you

Implement -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
and store the URL from the NSURLRequest in an instance variable. Then simply access this variable in your goBack method. If you're multiple navigation steps in, use an array to store each subsequent request and then just access the object at index 0 to get the original URL.

Related

Switch views in Xcode when a link is clicked from a webview?

I have an app with localhtml inside a webview. The localhtml is basically a list with links to various sites. Is there a way to make one of these links direct to a new view inside the app? Creating a button inside the webview is not an option in this case. The idea I had was to set the link to "switchviews" or something then switch views if the link matched. So like this (just for the concept)
If link matches "switchviews" {
Switch view
}
Is the a valid way to do this and can you share the actual code for doing so? Or is there a more effective way?
You can use UIWebViewDelegate an look to the method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
There look for the NSURLRequest to known which link is going to be loaded or the UIWebViewNavigationType which tells you what kind of action is happening right now.

Submit a form in UIWebView

I have a form in the UIWebView to interact with a web service, when I submit the form, the data gets sent to a web service and it will return the result to another UIWebView. The two UIWebViews are under a navigation controller, so when the use submits the form, it slides to another view to display the result. Can anyone point me out how I can achieve this? What do I fill in the "action" attribute in the form element? How can I get the user input back to the application so I can use the web service? I am new to this field, any help will be appreciated. Thanks!
Update:
Is there a way to save form data from web view back to my program? I think it's better to save form data back to the program (i.e. store params into nsstring) when I click submit button, and then start querying web service.
You can use the
stringByEvaluatingJavaScriptFromString
function for firing a javascript method from objective c and get a return value from that method.
For example
//Calling the getTheUsername in the javascript.
NSString *userName = [yourWebView stringByEvaluatingJavaScriptFromString:#"getTheUsername()"];
and in javascript
//Method in javascript
function getTheUsername()
{
return userNameVariable;
}
And I doubt that here you may wanna do vice-versa (Call obj-c method from javascript). This cannot be done directly here is the work around.
Set a UIWebViewDelegate, and implement the method webView:shouldStartLoadWithRequest:navigationType:. In your JavaScript code, navigate to some fake URL that encodes the information you want to pass to your app, like, say:
window.location = "someLink://yourApp/form_Submitted:param1:param2:param3";
In your delegate method, look for these fake URLs, extract the information you need, take whatever action is appropriate, and return NO to cancel the navigation.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(#"%#",[[request URL] query]);
return YES;
}
If I understood your problem correctly, I think following steps will help you to provide the solution.
In the action of form, you need to write the URL to the web service.
In your form, the name of the input field should be matched with the name of the parameters expected for the web service.
In your first UIWebView Navigation control, Create the object of screen having second UIWebView and set the response of the web service to the instance member of the object.
In your first UIWebView Navigation control, you need to write
[self presentModalViewController:webView2Object animated:YES];
In the screen of having webView2, parse the response before loading the second UIWebView. and inject it with the displayed page using javascript.

url from webview iphone

Hi I just wanted to know how to extract url from webview after loading the web view with the method [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://m.nytimes.com"]]];
In this, I am loading the page with nytimes. When the user navigates to the new page I wan to find out the url the moved page. Is there any way I can find it?
Thanks
You will need a class to implement the UIWebViewDelegate Protocol, then set an instance of that class to be the delegate of the webview. The method you will want to implement is:
- (void)webViewDidFinishLoad:(UIWebView *)webView
In that delegate method, you can get the url by inspecting the request property of the web view.
Edit: Changed the recommended delegate method due to Anomie's comment pointing out my failure to see the request propery of UIWebView.

Updating address bar with current url from UIWebview

Like iPhone safari browser, I want to update UItextview with current url of UIWebView
I tried shouldStartLoadWithRequest delegate method, but I end up with url of some ads/iframe
If I use webViewDidFinishLoad delegate method, then address bar gets updated at the end of the request.
Please help me on the best approach.
You want to use the mainDocumentURL method on the NSURLRequest in webView:shouldStartLoadWithRequest:. This should contain the main document URL regardless of whether the page is loading or a resource on the page (e.g. a frame) is loading.
Use webviewDidFinishLoad when delegated from your webview, and then query it for the title with [webview stringByEvaluatingJavaScriptFromString:#"document.title"] or weboutWebView.request.URL.absoluteString for the url.

How to open any link from a UIWebView to a new UIWebView?

I want to make a simple app, where a UIWebView with custom content will have several links to other pages with similar content as well (and a navigation bar on top, with just a back button).
I read the answers to this question, however I'm not sure if I should do that in my application, as the user might be able to go deep enough, and I will be creating new webviews all the time.
What could be the best practice for such a behavior?
Thank you!
I'd recommend listening for:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
and always returning YES (so the webView will continue with the requested link) while storing each request in an NSMutableArray to create a stack of the user's browsing history.
That would let you update a back button's text with the previous page's title (shortened of course).
If you just need to have a back button without label, you could have a simple button hooked up to the UIWebView's - (void) goBack; method.