我有一个 NSURLSessionDownloadTask 和一个 backgroundSessionConfigurationWithIdentifier。
当我锁定屏幕时,出现此异常:
Error Domain = NSPOSIXErrorDomain Code = 1 "The operation could not be completed Operation not permitted.".
此错误仅出现在我的手机上,不会出现在其他手机上。
下面是简单的代码:
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];
最佳答案
我最近遇到了同样的问题。我发现响应被保存到缓存目录中的一个文件中,由于用户有密码,该文件将被锁定。在创建 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];
关于ios - BackgroundSession SessionDownloadTask 锁屏时报错: NSPOSIXErrorDomain Code = 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31260125/