2017-07-27 248 views
0

我在應用程序中有一個計時器。要重現該問題,用戶應該啓動此計時器,然後將應用程序輸入背景或關閉屏幕。該應用程序工作長達5分鐘,然後被系統殺死。用戶重新打開屏幕後,可以看到主屏幕。如果要再次啓動應用程序,它將從頭開始,但不會從用戶在關閉屏幕之前的位置開始。iOS 10.3.2停止〜5分鐘後應用程序後臺工作

我試着在我的AppDelegate以下解決方案來解決這個問題:

- (void)methodToRepeatEveryOneSecond 
{ 
    if (self.flag) { 
    dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); 
    double delayInSeconds = 1.0; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
    dispatch_after(popTime, q_background, ^(void){ 
    [self methodToRepeatEveryOneSecond]; 
    }); 
    } 
} 
-(void)applicationDidEnterBackground:(UIApplication *)application{ 
    NSUserDefaults* def = [[NSUserDefaults alloc] init]; 
    if ([[def valueForKey:@"status" ] boolValue]) { 
    self.flag = true; 
    dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); 
    dispatch_async(q_background, ^{ 
     [self methodToRepeatEveryOneSecond]; 
    }); 
    } 
} 
-(void)applicationWillEnterForeground:(UIApplication *)application{ 
    self.flag = false; 
} 

但仍應用後停止工作〜5分鐘的背景。

當我在USB電纜應用程序的幫助下測試此解決方案時,在後臺甚至可以運行30分鐘,但在大約4分鐘的後臺工作後,我可以在控制檯中看到以下日誌(這裏是它的一部分):

2017-07-26 17:43:14.496379+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16df86530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. 
2017-07-26 17:43:14.496493+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e1b6530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. 
2017-07-26 17:43:14.496790+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e012530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. 
2017-07-26 17:43:14.497065+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16df86530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. 
2017-07-26 17:43:14.497188+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e242530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. 
2017-07-26 17:43:14.497476+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e242530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. 

但是如果與來自testflight下載的幫助下測試相同的身材,我得到的問題,我上面寫的(與主屏幕的顯示)。

此外,我嘗試使用支持位於我的Info.plist文件中的UIBackgroundModes鍵中,它工作正確,但測試版應用程序審查被拒絕,出現«Guideline 2.5.4»問題。

請給我意見時,我可以使用時,應用程序是在您的應用程序自行終止閒置3分鐘的背景來解決這個問題

+0

您的應用程序的目的是什麼以及您使用了哪種背景模式? –

+1

@mag_zbc應用程序適用於音樂人。他們在計時器下排練。所以我需要它一直工作,除非它們會關閉它(即使屏幕關閉或應用程序在後臺) 後臺模式:後臺獲取。 另外我試過使用地理位置,爲此我使用位置更新後臺模式 – Simon

回答

-1

,你失去了你所有previliges和您的應用程序不能做任何事情,但聽取獲得通知(如果你有通知實施)

如果你想禁止你的應用程序終止,你可以讓它在該背景下運行一個沉默的MP3,這樣你的應用程序總是激活(消耗電池加上將被拒絕)。

但是,如果用戶雙擊主頁按鈕並向上滑動您的應用程序(終止它)。沒有什麼可以做的!

希望這會有所幫助!