开发者

iPhone Webview onBackground event?

开发者 https://www.devze.com 2023-02-07 18:43 出处:网络
I am developing a iphone app using phonegap. Now I want to maintain a session state when the app goe开发者_JAVA百科s into the background. I want to persist that state even if the app gets killed by th

I am developing a iphone app using phonegap. Now I want to maintain a session state when the app goe开发者_JAVA百科s 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


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.


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:


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" :(

0

精彩评论

暂无评论...
验证码 换一张
取 消