简体   繁体   中英

The NSDATA is not writing into Documents Dir, when iPhone on Background mode with Locked State?

I tried following methods:

在此输入图像描述

  1. In Appdelegate.m

     -(void)applicationDidEnterBackground:(UIApplication *)application { __block UIBackgroundTaskIdentifier taskId = 0; taskId = [application beginBackgroundTaskWithExpirationHandler:^{ taskId = UIBackgroundTaskInvalid; }]; } 
  2. Actually I am using NSURLSession to download the File

     -(void)methodForDownloadTheFile:(NSString *)fileName { // 1 NSString *fileApi = @"https://api-content.dropbox.com/1/files/dropbox"; NSString *escapedPath = [fileName stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; NSString *urlStr = [NSString stringWithFormat: @"%@/%@", fileApi,escapedPath]; NSURL *url = [NSURL URLWithString: urlStr]; [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; if (self.dataTask) { [self.dataTask cancel]; } // 2 self.dataTask =[_session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (!error) { NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response; if (httpResp.statusCode == 200) { // 3 // [_filleddata appendData:data]; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *localPath = [documentsPath stringByAppendingPathComponent:fileName]; if( [data writeToFile:localPath atomically:YES]) { NSLog(@"HOpefully written into Documentz directory.."); } else { NSLog(@"Writting file mechanism - Failed!"); } dispatch_async(dispatch_get_main_queue(), ^{ [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; NSLog(@"Downloaded Successfully"); }); } else { // HANDLE BAD RESPONSE // } } else { // ALWAYS HANDLE ERRORS :-] // } // 4 }] ; if (self.dataTask) { [self.dataTask resume]; } } 

And Even The Delegate - Downloading Progress ALSO NOT getting fired...

-(void)URLSession:(NSURLSession *)session
     downloadTask:(NSURLSessionDownloadTask *)downloadTask
     didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
  NSLog(@"%f / %f", (double)totalBytesWritten,
    (double)totalBytesExpectedToWrite);
}

You have data protection enabled in your capabilities, so the documents directory will not be accessible while the device is locked unless you flag it appropriately .

Have a look at NSFileProtectionNone attribute and the setAttributes:ofItemAtPath:error: method in NSFileManager .

Or, if you don't require data protection (ie the data in your app isn't considered sensitive) turn off the data protection capability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM