2015-12-09 26 views
0

我有一個應用程序,我想每次都在後臺工作,並且只有當用戶關閉(終止)它失敗(如每日警報)時。我的代碼是:當設備鎖定時,NSTimer沒有工作

的AppDelegate:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
    { 
     backgroundUpdateTask = 0; 
     return YES; 
    } 

    - (void)applicationWillResignActive:(UIApplication *)application{ 
    } 

    - (void)applicationDidEnterBackground:(UIApplication *)application{ 

backgroundUpdateTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:^{ 
     [self endBackgroundUpdateTask]; 
    }]; 
     if (application.applicationIconBadgeNumber > 0) { 
      application.applicationIconBadgeNumber = 0; 
     } 
    } 

    - (void)applicationWillEnterForeground:(UIApplication *)application{ 
     [self endBackgroundUpdateTask]; 

     if (application.applicationIconBadgeNumber > 0) { 
      application.applicationIconBadgeNumber = 0; 
     } 
    } 

    - (void)applicationDidBecomeActive:(UIApplication *)application{ 
     lte = [[NSUserDefaults standardUserDefaults] valueForKey:@"LTE"]; 
     if (lte == nil) { 
      [Utility GetNewNotification:lte]; 
      lte = [[[notifyDic objectForKey: @"Notification"]valueForKey:@"LTE"]valueForKey:@"text"]; 
      [[NSUserDefaults standardUserDefaults] setValue:lte forKey:@"LTE"]; 
     } 
    } 

    - (void)applicationWillTerminate:(UIApplication *)application{ 
    } 

    - (void)endBackgroundUpdateTask{ 
     [[UIApplication sharedApplication]endBackgroundTask:backgroundUpdateTask]; 
     backgroundUpdateTask = UIBackgroundTaskInvalid; 
    } 

和視圖的Controler:

@implementation ViewController 
{ 
    NSTimer *myTimer; 
    int counter; 
} 

-(void)CheckTimer{ 
    if(counter == 0){ 
     counter = 60; 
     NSString* lte = [[NSUserDefaults standardUserDefaults] valueForKey:@"LTE"]; 
     [Utility GetNewNotification:lte]; 
    } 
    else { 
     counter --; 
     //do your video playing work here 
    } 
} 

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    /// Counter for get new notification 
    counter = 60; 
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(CheckTimer) userInfo:nil repeats:YES]; 
} 

我想每60秒計時器是觸發和服務調用。但在應用程序轉到後臺計時器後的180秒後關閉。並且如果設備是鎖定定時器也是關閉的。

+0

由於iOS版7.1可以運行在後臺3分鐘,如果你想比最高可以使用'[UIApplicationsharedApplication] beginBackgroundTaskWithExpirationHandler:^ { }]運行10分鐘就可以' –

+1

你可能想了解更多關於iOS背景執行效果如何__ [來自最佳](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html)__ _(劇毒警報:蘋果)_。 – holex

+0

我閱讀每一個文件,我知道這一切。但要超過3分鐘或10分鐘的應用程序(如鬧鐘) – Sina

回答

6

要在後臺運行的代碼參照該鏈接: - https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

注: -

  1. 應用程序只能拿到後臺執行的10分鐘 - 這一次持續時間後,計時器將停止射擊。
  2. 從設備鎖定時的iOS 7或更高版本開始, 將幾乎立即掛起前臺應用程序。在iOS 7或更高版本的應用程序被鎖定後,計時器不會觸發。
0

@Darshan Mothreja對他的回答是正確的。如果你想在後臺運行代碼,你只有有限的時間去實際做事。在這段時間過後,你的運氣一直不好,直到你的應用程序回到前臺。

取決於你想完成什麼,你可能想看看AppDelegate中的生命週期方法。

特殊照顧可能會想看看方法:

- (void)applicationDidBecomeActive:(UIApplication *)application; 

,並添加一些更多更新代碼給它。這將允許您在應用程序返回到前臺時更新事物或獲取新數據。

相關問題