2011-07-21 248 views
0

大家使用下面的代碼後臺任務,它工作正常,當iPhone與Xcode的連接,但是當我跑不連接的Xcode的應用程序,那麼後臺任務將無法正常工作後臺任務只運行

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    back=1.0f; 

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; 
    NSRunLoop *runLoop=[NSRunLoop currentRunLoop]; 
    timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeCounter) userInfo:nil repeats:YES]; 
    [runLoop run]; 
    [pool release]; 
} 

請幫助爲什麼發生這種情況

回答

2

你檢查的背景executation的文檔?

你應該開始像任務:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    UIApplication* app = [UIApplication sharedApplication]; 

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Do the work associated with the task. 

     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }); 
} 

http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html