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

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.

Related

iOS get destination url before uiwebview goBack

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.

How can I add multiple webviews (on different pages) to one app? (iPhone/iPad)

I'm trying to add several UIWebViews (on different pages) in my iPhone app, I added the UI part of it, but I can't figure out how to program them to load separate pages. I watched a YouTube video on how to add a webview, and the method works for a single webview, but when I try to add another to the detail view delegate, it tells me that I'm re-defining a couple different items. Anyone know how to fix this?
All the UIWebView delegate methods pass you a reference to the UIWebView instance that the delegate call is relevant for.
So for example if you have two UIWebViews called webView and webView2 then you can do something like this:
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if (webView == self.webView1) {
// Do something for webView1
}
else if (webView == self.webView2) {
// Do something for webView2
}
}
Same applies to all other delegate methods for UIWebView.

A Label in View with UIWebView

I have a View, i would like to put a test ( a UILabel) witch when clicking it, it open me a web View with an URL ( WWW.mySite.com).
How i can do this please ?
thanks for your answers
Use a button, put the text into the button, hook up the actions from the button pressedUpInside to a function which will call the UIWebView's - (void)loadRequest:(NSURLRequest *)request with your URL.
Look up Apple documentation on UIWebView and use a UIButton connected to an IBAction. Always turn to the documentation when these kinds of questions come up. It is really through and usually pretty well done.

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.

UIWebView finished loading Event

Is it possible to start an event when an UIWebView (Iphone) has finished loading the URL.
How can I find out, the current URL of the UIWebView?
Yes, that's possible. Use the UIWebViewDelegate protocol and implement the following method in your delegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView
If you want the URL, you can get the last request using the property request:
webView.request.URL
None of the found solutions worked for me.
Then I found this example which at least works much better than any other solution I found on Google/StackOverflow.
uiwebview-load-completion-tracker
Very simple method:
Step 1: Set delegate UIWebViewDelegate in header file.
Step 2: Add following webViewDidFinishLoad method to get current URL of webview
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"Current URL = %#",webView.request.URL);
//-- Add further custom actions if needed
}
Pascal's answer for the "getting the URL" part is fine.
However!
From UIWebViewDelegate's documentation, from Apple:
"webViewDidFinishLoad: Sent after a web view finishes loading a frame."
Frame != Page.
webViewDidFinishLoad is called when the page is "done loading". It can also be called many times before then. Page loads from Amazon.com can generate a dozen calls to webViewDidFinishLoad.
If you control the page source, then you can make a load test for it, and it will work, for that case. If you only care about getting called "after the page is done loading", then webViewDidFinishLoad is adequate.
For arbitrary pages, with arbitrary JavaScript, loading ad banners in perpetuity, or autoscrolling banners, or implementing a video game, the very idea of a page being "done loading" is wrongheaded.