BackgroundSession SessionDownloadTask when locking screen, Error: NSPOSIXErrorDomain Code = 1
BackgroundSession SessionDownloadTask when locking screen, Error: NSPOSIXErrorDomain Code = 1
我有一个 NSURLSessionDownloadTask
和一个 backgroundSessionConfigurationWithIdentifier
。
当我锁定屏幕时,出现此异常:
Error Domain = NSPOSIXErrorDomain Code = 1 "The operation could not be
completed Operation not permitted.".
此错误仅出现在我的 phone 上,不会出现在其他 phone 上。
下面是简单的代码:
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];
我最近遇到了同样的问题。我发现响应被保存到缓存目录中的一个文件中,由于用户有密码,该文件将被锁定。在创建会话之前,您需要 运行 在应用中的某处执行此操作。
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];
James 对 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)
}
我有一个 NSURLSessionDownloadTask
和一个 backgroundSessionConfigurationWithIdentifier
。
当我锁定屏幕时,出现此异常:
Error Domain = NSPOSIXErrorDomain Code = 1 "The operation could not be completed Operation not permitted.".
此错误仅出现在我的 phone 上,不会出现在其他 phone 上。
下面是简单的代码:
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];
我最近遇到了同样的问题。我发现响应被保存到缓存目录中的一个文件中,由于用户有密码,该文件将被锁定。在创建会话之前,您需要 运行 在应用中的某处执行此操作。
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];
James 对 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)
}