使用iPhone應用程序聊天的Iam使用套接字連接與服務器進行通信。當應用程序移動到後臺時,我可以看到服務器能夠與應用程序通信約5分鐘。但在此之後,套接字連接被破壞。但是,應用程序一旦移動到後臺就會停止執行。爲什麼套接字連接保持5分鐘,而不是應用程序執行。蘋果是否指定了連接的確切時間。iOS應用程序轉到後臺後導致套接字連接中斷
回答
您可以通過使用利用的下面的代碼在applicationDidEnterBackground獲得600秒(10分鐘)的最大時間:
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
//System will be shutting down the app at any point in time now
}];
//Background tasks require you to use asyncrous tasks
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Perform your tasks that your application requires
NSLog(@"\n\nRunning in the background!\n\n");
[application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
});
}
}
我剛剛實施的backgroundTaskIdentifier對象並使無效background_task檢查時間,應用程序是活着的,並運行600秒。您甚至可以通過使用此功能獲得剩餘時間
NSLog(@"Time remaining: %f", application.backgroundTimeRemaining);
如果您的目標是iOS 4.3或更高版本,則不需要兩個初始if語句。 – rmaddy 2013-02-14 18:53:53
@rmaddy你是對的:) – 2013-02-15 04:32:08
@chinta在這裏可以找到配置VoIP使用套接字的背景。非常翔實的http://wiseman-safiq.blogspot.in/2010/11/ios-executing-code-in-background.html – 2013-02-15 04:37:28
從蘋果公司的IOS Programming Guide
是進入後臺時,大多數應用程序移動到 暫停狀態之後不久。在此狀態下, 應用程序不會執行任何代碼,並且可能隨時從內存中刪除 。爲用戶 提供特定服務的應用程序可以請求後臺執行時間,以便提供這些服務。
這至少可以解釋爲什麼應用程序停止執行。爲什麼您的服務器仍然能夠與您的應用程序通信5分鐘,可能是因爲您設置了一個額外的很長時間,並且沒有在您的應用程序進入後臺時明確關閉套接字連接。
- 1. iOS外部附件在應用程序轉到後臺時斷開連接
- 2. 在後臺連接Android套接字
- 3. 當應用程序轉到後臺時網絡連接丟失
- 4. Linux後臺程序等待套接字連接
- 5. 如何在會話斷開後重新連接到後臺GUI應用程序?
- 6. iOS CoreBluetooth在後臺重新連接應用程序
- 7. C#:套接字問題 - 幾秒鐘後連接的套接字斷開連接
- 8. 當應用程序轉到後臺時維持套接字連接的最佳方式
- 9. 中斷連接套接字
- 10. Android始終在後臺服務中使用套接字連接
- 11. 斷開連接後,Android BLE在後臺連接
- 12. 斷開連接後的Socket.io套接字數據可用性
- 13. C STREAM客戶端套接字在斷開連接後重用
- 14. Android:USB斷開連接導致應用程序崩潰
- 15. 後臺線程導致應用程序崩潰後無錯誤
- 16. Quickblox iOS:在應用程序處於後臺時連接到QBChat錯誤
- 17. 導致連接的套接字在.BeginReceive之後立即接受新消息?
- 18. 當應用程序在ios中後臺時接收voip電話
- 19. 如何保持活動連接到服務器通過套接字當應用程序在後臺模式?
- 20. 連接到接受套接字連接的Java EE應用程序
- 21. Android應用程序在後臺關閉連接線程
- 22. ASIHTTPRequest在中斷Internet連接後導致崩潰
- 23. 斷開連接後訪問套接字對象
- 24. (iOS版MPMusicPlayerNotification)如何,即使應用程序轉到後臺時收到通知應用程序在後臺
- 25. 從Android應用程序連接到套接字
- 26. Android - 如何連接到應用程序的抽象套接字
- 27. Windows Phone 7的放在後臺時(芒果)應用程序斷開連接?
- 28. React原生socket.io在應用程序處於後臺模式時斷開連接
- 29. 如何在後臺維護VOIP套接字連接?
- 30. Android套接字在連接後卡住
您的應用程序是否已註冊爲VoIP應用程序? – trojanfoe 2013-02-14 14:00:23
nope。它沒有註冊爲Voip。 – Chinta 2013-02-20 04:02:33