2011-11-22 71 views
4

這是我的代碼暫停事件在PhoneGap iPhone中無法正常工作?

//This is an event that fires when a PhoneGap application is put into the background. 
    document.addEventListener("pause", onPause, false); 

    //This is an event that fires when a PhoneGap application is retrieved from the background. 
    document.addEventListener("resume", onResume, false); 

    // Handle the pause event 
    function onPause(){ 
    console.log("pause : app is put into background"); 
    } 


    // Handle the resume event 
    function onResume() { 
    console.log("resume : app is put into foreground"); 
    } 

當我按下home鍵沒有登錄控制檯但是當我點擊相關應用(使它在前臺),那麼我的日誌

2011-11-22 12:11:37.206 Event[644:207] [INFO] pause : app is put into background 
2011-11-22 12:11:37.206 Event[644:207] [INFO] resume : app is put into foreground 

我不知道爲什麼暫停函數在前臺出現時被調用。
有什麼我做錯了嗎?

+0

我在Android上的同樣的問題。 – Toubey

回答

6

這是從文檔

iOS Quirks

In the pause handler, any calls that go through Objective-C will not work, nor will any calls that are interactive, like alerts. This means that you cannot call console.log (and its variants), or any calls from Plugins or the PhoneGap API. These will only be processed when the app resumes (processed on the next run-loop).

+0

我有同樣的問題。我明白console.long不會工作時,我按Home按鈕,以儘量減少應用程序,但當我恢復它,應該有一個消息對嗎? –

+0

是的根據文檔 –

+0

它似乎並沒有爲我工作是否有辦法我可以檢查什麼可能是錯的?我也試過在documentready上使用console.log,但那也不起作用,所以也許我做錯了什麼。我在這裏粘貼了我的代碼,以查看是否可以發現真正有用的問題。 http://jsfiddle.net/6QXY7/ –

2

我懷疑實際發生的情況是,來自暫停事件的console.log()並沒有在簡歷中被觸發,因爲只是系統無法輸出console.log(),直到它恢復爲止。

PhoneGapDelegate.m中的Objective-C方法觸發了暫停事件(applicationWillEnterForeground:(UIApplication *)application),將其發送到JavaScript,但當時該應用程序在後臺並處於暫停狀態。 JavaScript在重新進入前臺之前無法接收事件。

爲了驗證這一點,只需後臺您的應用程序的時間較長時期...它應該然後引起他們的錯誤:

void SendDelegateMessage(NSInvocation*): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode

這似乎是在PhoneGap的一個bug。也許你可以提出一個問題:https://github.com/callback/callback-ios

+0

感謝您的建議....我會檢查它,並會嘗試得到確切的答案..我沒有得到任何錯誤,當應用程序在後臺很長一段時間...也是這種方法'void SendDelegateMessage(NSInvocation *):委託(webView:decidePolicyForNavigationAction:request:frame:decisionListener :)' –

+0

也許這個錯誤是特定於我的測試應用程序。我在https://github.com/callback/callback-ios/issues/17上添加了一個可能有幫助的問題。目前沒有爲「applicationWillResignActive」或「applicationDidBecomeActive」觸發事件。這些可能會更好地停止定時器等,當應用程序不活動。 – Devgeeks