What method gets called before a WebView is closed by owner window? - objective-c

I'm working with a webview within a window of a Cocoa application. I would like to retrieve some values using stringByEvaluatingJavaScriptFromString on the webview and save them before the application shuts down. I tried using the applicationWillTerminate but by the time I reach this method, it is too late. I was wondering if there's something built into webview that I've missed or if anyone has an elegant solution to share.

If you want to save this information, why are you waiting until the app is about to terminate?

Related

Handling WebView having loaded?

I'm writing a browser application for Mac to match my Windows one. I have a WebView on my window and so far I have a working browser. However, I need to run a method I've written when the WebView finishes loading a page. Is there a way to do this? If not, how can I work around that? Thanks!
You can call your method in the webView:didFinishLoadForFrame: delegate call that is in the WebFrameLoadDelegate.

applicationWillTerminate not getting called on force quit of iOS app

Does anyone have any insights into when/under what conditions applicationWillTerminate is called in iOS 5/6?
I've got some logic i'd like to execute whenever the application terminates (not moves to the background), for example if the user navigates to the application bar at the bottom of the screen by double tapping the home button and force quits the app.
when i try to do this on a test device, applicationWillTerminate does not seem to get called. Is there a reason for this?
My plan B is to tie that logic to some persistent object like a singleton or a static that is automatically destroyed when the app quits.
Any suggestions?
thanks
Have you read the documentation for applicationWillTerminate:,
It says,
For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.
There is a "maybe" mentioned there. Probably that answers your question. So it is not necessary that this will get called when you quit the app. Probably you might have to use UIApplicationExitsOnSuspend to disable multitasking and then it might get called while putting in background. But that again depends on your app requirement. If you cannot disable multitasking, you might have consider doing that in applicationDidEnterBackground method or so. I am not sure if there are any other delegate methods which will help in identifying the force quit.

After exiting app, how to go back to the exact same screen where I left off?

Is there a quick and easy way to have the app go back to the same screen after I put it in background mode?
I know there are frameworks just for this stuff, but has anyone done it without much sweat?
Thanks
In your app delegate, add a line to the applicationDidenterBackground: method that stores the current page via NSUserDefaults.
Then in the applicationWillEnterForeground: method, load up that saved value and restore the page.
Multitasking should enable this, while a device has enough memory to keep your app open. Otherwise how about using NSUserDefaults to store a string reference to your view?
As of iOS 5.1, there is no quick and easy way.

iOS - Asynchronous Image Downloading

I am writing an app which is going to be displaying images found on my server in a UIImageView.
I need something that will asynchronously download the image and cache it while putting it in the UIImageView.
The download also needs to be able to be cancelled when I press a button.
Can anyone point me in the direction of something that can do this?
In the old times the framework for that was ASIHTTPRequest but is an abandoned project now. This https://github.com/AFNetworking/AFNetworking seems to be popular now.
Use the performSelectorInBackground:withObject: - method from NSObject ;)
Advise: The updating of the view must run on the main thread!
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html

Problem: restarting App

My App is a view-based application. At the beginning I show my logo and after a delay of a few seconds it changes into another view. from then on the user can switch to a lot of different views.
Sooooo.. My Problem: The thing is, when I restart my App. [..well closing and reopen by touching the icon..] the app itself doesnt restart in the sence of jumping to the very first view. to the contrary: at restart the user just returns to the last view that was open.
So I dont know why this is.
Is it normal to somehow manually tell the app to return to the very first view after restart? And if so, how do I have to do that?
PS.
I have so no idea what to do.. Maybe my problem has to do with the timer i used in the first view to change after a delay of time?
Please, is there anyone, who can help me?
Your problem is that, as of iPhone 4, returning to the home screen does not terminate your app. It's just made inactive, so opening it again reactivates it. In most cases, this is a good thing. If it doesn't work for your app, you can add the UIApplicationExitsOnSuspend key to your Info.plist with a value of YES.
(As I said, you should only do this if it really helps usability. If it's just about getting your splash screen shown again, most users and possibly Apple will frown upon it.)
iOS 4.0 and greater have a fast-start thing that allows apps to restart back from where they were upon restarting. There are several ways to deal with this:
1.) your App Delegate receives info about being but into the background and resumed. - (void)applicationDidBecomeActive:(UIApplication *)application and - (void)applicationDidEnterBackground:(UIApplication *)application are the relevant functions here. Check the docs.
2.) You can also disable the background, inactive state completely by including UIApplicationExitsOnSuspend in you Info.plist as Chuck already pointed out.
Overall, you should check the application state docs on Apple's Side.