2013-05-03 5 views
0

내 앱에 백그라운드 작업을 구현하려고합니다. 구현하기 전에 아래와 같이 간단한 예제를 제공하고자했습니다. 나는 방금 카운터를 놓고 그것이 중요한지 아닌지보고 싶다. 그러나 그것은 프로세스라고 불리는 내 메소드를 시작하지 않습니다. 나는 내가 무엇을 놓치고 있는지 궁금해. 코드 아래ApplicationDidEnterBackground가 메서드를 실행하지 않습니다.

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    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"); 
       pollingTimer2 = [NSTimer scheduledTimerWithTimeInterval:0.1 
                   target:self 
                   selector:@selector(process) 
                   userInfo:nil 
                   repeats:YES]; 
       [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 
      }); 
     } 
    } 

} 

-(void) process 
{ 
    county= county+1; 
    NSLog(@"%d,county",county); 

} 
+1

endBackgroundTask를 주석 처리하고 –

+0

을 시도하십시오. 응용 프로그램을 .plist 파일에서 background = YES로 실행하지 마십시오. –

답변

0

사용이 :

if ([[UIDevice currentDevice] isMultitaskingSupported]) 
{ 
     //Check if device supports mulitasking 
     UIBackgroundTaskIdentifier bgTask; 
     UIApplication *app = [UIApplication sharedApplication]; 
     bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
      [app endBackgroundTask:bgTask]; 
     }]; 
     pollingTimer2 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(process) userInfo:nil repeats:YES]; 
} 

그것의 작업과 그것을 테스트했습니다.

관련 문제