BackgroundSession SessionDownloadTask when locking screen, Error: NSPOSIXErrorDomain Code = 1
Asked Answered
P

2

7

I have a NSURLSessionDownloadTask with a backgroundSessionConfigurationWithIdentifier. When I lock the screen, this exception occurs:

Error Domain = NSPOSIXErrorDomain Code = 1 "The operation could not be completed Operation not permitted.".

This error occurs only on my phone, it does not appear on other phones.

below is the simple code:

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.edu.downLoadTest"];;
AFURLSessionManager *_session = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://podcasts.apple.com/apple_keynotes_hd/2015/2015_mar_hd_cc.m4v"]];

NSProgress *progress;
NSURLSessionDownloadTask *task1 = [_session downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSString *a =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    return [NSURL fileURLWithPath:[a stringByAppendingPathComponent:@"test.m4v"]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"%@",error);
}];


[task1 resume];
Philippine answered 7/7, 2015 at 5:4 Comment(2)
I have noticed this problem too. It seems to only reproduce when the phone has a lock screen enabled.Eboh
It seems like this might be the same problems as this question: #25788322Eboh
F
7

I have recently come across the same issue. I have found that the response is save to a file in the Cache Directory which will be locked due to the user having a password. You need to run this somewhere in the app before you create the session.

  NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    path = [path stringByAppendingPathComponent:@"Caches/com.apple.nsurlsessiond/Downloads"];


    NSString *appInfoPlist = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:appInfoPlist];

    path = [path stringByAppendingPathComponent:dictionary[@"CFBundleIdentifier"]];
    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionCompleteUntilFirstUserAuthentication} ofItemAtPath:path error:nil];
Finnell answered 28/9, 2015 at 11:3 Comment(0)
C
2

Just a quick translation of James' answer to Swift 4

if let plist = Bundle.main.path(forResource: "Info", ofType: "plist"),
   let dict = NSDictionary(contentsOfFile: plist) as? [String: AnyObject],
   var path = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first,
   let bundle = dict["CFBundleIdentifier"] {

    path.append("/Caches/com.apple.nsurlsessiond/Downloads/\(bundle)")
    try? FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication], ofItemAtPath: path)
}
Cranmer answered 26/10, 2018 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.