iphone - how to mentain the state of UIWebView? - iphone

i am having a UIWebView showing and HTML page that has some checkboxes, radio buttons. My Application has tabbar controller that switches b/w two controllers. How i can maintain the current state of UIWebView. ( e.g. i have selected a checkbox and i change the tab and go to other ViewController and again come back to UIWebView. the WebView resets itself and goes to the start... i want to maintain the state of UIWebView ( all the checkboxes clicked) .. how i will do that ??
any idea ??
thanks in advance

It sounds like you're loading your web view in viewDidAppear:, or some other method that gets called each time you switch to the tab. If you load your web view in viewDidLoad:, it will only get loaded once, and will retain its state when you switch tabs.
EDIT: When I wrote this answer back in 2009, I apparently didn't understand the view controller life cycle as well as I thought I did. Corey Floyd was right. The viewDidLoad: method can also get called multiple times, because UIViewController will unload its view in low memory situations if the view isn't being displayed. When the user switches back to the view, viewDidLoad gets called again to reconstruct the view.
How embarrassing.

I have an idea that might help:
to get the status from your app to the webpage, give the variables' values as request parameters with the called URL and let the webpage handle them and view the form elements accordingly.
to get the status from the webpage back to your app, let the webpage be refreshed each time any of its element is updated, and with the new URL give the new values of the changed elements. Back to your app, you can implement the UIWebViewDelegate and inside the method WebViewDidFinishLoad (or WebViewDidStartLoad as you prefer) use the webView.request.URL to parse the GET parameters and update your app accordingly.
hope this will help.

Related

Iphone knowing when going back in navigational view control

I'm using a navigation based view controller on my iPhone app that uses an RSS feed on-load of a nib to bring back results, then another xml call when you select one. Is there an action when you go back on the second view controller to the first, the reason I ask is that it's reloading the first results again (going off to the server) as it's in the viewdidappear. How do I tell it not to re-load the results?
Tom
Move the code to the ViewDidLoad it is only called when the view gets loaded.

Reload View only when application is opened Again

My application has tab bar with one of the tabs having a segmented control on navigation bar.
Based on segment clicked different view is displayed using a url.
I am calling the url in viewdidload method.I do not want to use viewwillAppear to call the url as it will be called each time the view is displayed.
I only want to call the url again whenever user closes the application and comes back.
Whats the best way to do this.Should I remove the view controller from and reload it again once the application is opened.
Please use a more descriptive title for your question next time!
There are notifications for this that you can observe in your application:
UIApplicationDidBecomeActiveNotification and UIApplicationWillEnterForegroundNotification come to mind.

Good design for having an ActivityIndicator across entire app

I have the need for an activity indicator view in my app when different views are loading and when data is being retrieved. The problem is the mainVC (where I would place the indicator) is not always aware of when processing is happening so it can start the indicator but it cannot stop it.
e.g. the mainVC loads and then programatically adds a new VC - this VC in turn asks a model to retrieve - it displays data etc. So this newly added VC actually knows when processing is finished and it does not have access to the indicator view (although the indicator is visible at the top).
I was thinking of using notifications - is this the best way of handling this situation?
I'd recommend looking at the brilliant MBProgressHUD library:
https://github.com/matej/MBProgressHUD
It's a very simple set of classes you can use to display loading and progress views that can be accessed by all view controllers in your app. Basically, you can set it up in your app delegate and add it to your app window.
Every view controller can then access the progress view from the delegate and show/hide it when required. It comes with an example project and code - it's very easy to use and customise.
Notifications are one half of the solution. You have to combine them with a persistent object so that you can also get the current state at all times. E.g., when a view controller is about to appear, it needs to read the initial "downloading" state from somewhere, because the VC might have been created after the "start" or "end" notification was sent.
Then, while the VC is alive, it can simply respond to notifications to update the indicator.
This design is particulary important for views, which run the risk of getting unloaded/reloaded all the time.

Loading the content of a UIWebView before displaying it

I have a set number of ViewControllers in my app. They are allocated and initialized at application launch and released at exit. They are used by a NavigationController to be pushed/popped. In these ViewControllers there are WebViews (actually there is nothing else).
My problem is :
When I want to change de content (URL) of a WebView that is not on the current TopViewController, the content isn't loaded until I push/pop the associated ViewController.
And the transition us "ugly". The pushed/popped Viewcontroller shows old content at worst or blank page at best, before the ViewController is in full view THEN the new content is shown.
I tried lots of things (even putting the "loadRequest" in a different thread with looks stupid).
Do you know any way to make things go smooth?
In general you should plan your UI for slow UIWebView loads, because you can't plan ahead to know how long things will take. What I often do is have a UIView that has a spinner and a message such as "Loading..." that is layered on top of the UIWebView. Here's how I use it:
In viewWillAppear: I unhide the loading view and start the spinner.
In webViewDidFinishLoad I hide the loading view.
If webViewDidFail gets called, then you're not showing some interim or invalid content while the page loads. You can present a UIAlertView in that case.
If you're curious, you can see this in the high score page of my game (Lexitect, free)

UIwebview delays loading

I have Login screen and second view which will be shown after login. On second view I have UIWebview which loads the url.
After login first it shows the empty view and slowly it loads the url. I want to show login screen with wait cursor, until the uiwebview loads the url and then want show that screen.
Can any body please provide code or sample for this?
Regards,
Malleswar
I cannot write your application for you, because it is a large project.
However, you might think about making your own login UIView as a child view of a UIViewController, which contains form elements:
This view controller is the root view controller in a UINavigationController stack.
The user enters her credentials into this form.
On clicking Submit, you push another UIViewController on to the navigation stack that contains a UIView child, that itself has a UIProgressIndicatorView and a UIWebView as children views.
The progress indicator view does its spinning wheel thing while the web page loads with the user's credentials entered in step 2.
Once the web page finishes loading, it fires its delegate method telling you loading has finished. This delegate method tells the progress indicator view to stop spinning and hide.
I would recommend reading up on UINavigationController through Google and Apple's ample documentation and sample projects. One sample project template is included in Xcode, as well.