BackgroundSession SessionDownloadTasklocking屏幕时,错误:NSPOSIXErrorDomain Code = 1

我有一个带有backgroundSessionConfigurationWithIdentifierNSURLSessionDownloadTask 。 当我locking屏幕时,会发生以下exception:

错误域= NSPOSIXErrorDomain代码= 1“操作不能完成操作不允许”。

这个错误只发生在我的手机上,不会出现在其他手机上。

下面是简单的代码:

 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]; 

我最近遇到了同样的问题。 我发现响应被保存到caching目录中的一个文件,由于用户有密码而被locking。 在创build会话之前,您需要在应用程序的某个位置运行此应用程序。

  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];