iPhone Webview onBackground event?

702 views Asked by At

I am developing a iphone app using phonegap. Now I want to maintain a session state when the app goes into the background. I want to persist that state even if the app gets killed by the OS. (Just consider saving some variables in javascript when the app goes into background)

Is there some way I can do that? Does the UIWebView allows javascript to execute when the app is moving to background?

Thanks

3

There are 3 answers

3
Daniel Kurka On BEST ANSWER

I donĀ“t think that there is such an event, so I would suggest that you always save your state if you change it within your app. So you dont have to save while closing the app.

2
Jonathan. On

I don't know about phoneGap, but in objective-c you can add an observer (register for notifications for when the app goes into the background)

 [[NSNotificationCenter defaultCenter] addObserver:self
    selector: @selector(enteredBackground) 
    name: UIApplicationDidEnterBackgroundNotification
    object: nil];

Then create a method named enteredBackground, and you can put code like:

[myWebView stringByEvaluatingJavaScriptFromString:@"Do something in javascript"];

where you replace the Do something in javascript with the code you want to execute. See this: http://developer.apple.com/library/ios/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString:

0
Sailesh On

I think there no way to achieve this. If anyone has found a solution, please share.

All I am trying to do is get the "app-going-to-the-background" event in my webview so that I can run some javascript code at that point. It is similar to running objective-c code in applicationWillResignActivity. So if anyone has working example of how this can be done, please reply.

Otherwise, I will have to consider this as "cant-be-done-job" :(