NSURLSessionTask never calls back after timeout when using background configuration

后端 未结 5 900
面向向阳花
面向向阳花 2021-02-01 05:35

I am using NSURLSessionDownloadTask with background sessions to achieve all my REST requests. This way I can use the same code without have to think about my applic

5条回答
  •  离开以前
    2021-02-01 06:15

    There is one method in UIApplicationDelegate,which will let you know about background process.

    -(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
    

    If there are more than one session ,you can identify your session by

    if ([identifier isEqualToString:@"com.mydomain.myapp.mySessionIdentifier"]) 
    

    One more method is used to periodically notify about the progress .Here you can check the state of NSURLSession

    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
    
    
    NSURLSessionTaskStateRunning = 0,                   
    NSURLSessionTaskStateSuspended = 1,
    NSURLSessionTaskStateCanceling = 2,                   
    NSURLSessionTaskStateCompleted = 3,              
    

提交回复
热议问题